2014-04-17 153 views
1

我有两个通过展开segue连接的控制器。第一个视图有一个表格:Auction,Make,Model作为三行。当用户点击“拍卖”行时,它将它们发送到先前拍卖的桌面视图。通过展开segue传递变量

我想要做的是拍卖(从第二个视图点击)显示在第一个视图。我有展开segue工作,但是当我尝试创建一个静态变量来保存文本时,它会自动更改为NULL回头时。例如,我设置了一个名为'label'的staticNSString。当单击第二个tableview中的一行时,标签将更改为该单元格的文本值。但是,当我将其传回时,它将更改为NULL

这是第一个观点:

#import "searchCar.h" 
#import "auctionTable.h" 
@implementation searchCar 
@synthesize auctionLabel; 

- (IBAction)unwindToList:(UIStoryboardSegue *)segue 
{ 
    auctionTable *unwoundSegue = segue.sourceViewController; 
    self.auctionLabel.text = unwoundSegue.auctionName; 
} 

@end 

这里是第二个观点:

#import "searchCar.h" 
#import "auctionTable.h" 

static NSString *label; 

@implementation auctionTable 
@synthesize auctionName; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    //Default section data 
    [email protected][@"All"]; 

    //First section data 
    self.firstItemsArray = @[@"Houston, TX 4/2014",@"Davenport, IA 4/2014",@"Kissimmee, FL 1/2014",@"Las Vegas, NV 1/2014"]; 

//Second section data 
self.secondItemsArray = @[@"Kansas City, MO 12/2013", 
          @"Anaheim, CA 11/2013", 
          @"Davenport, IA 11/2013", 
          @"Schaumburg, IL 10/2013", 
          @"Dallas, TX 9/2013", 
          @"Monterey, CA 8/2013", 
          @"Walworth, WI 8/2013", 
          @"Santa Monica, CA 7/2013", 
          @"Des Moines, IA 7/2013", 
          @"West Allis, WI 7/2013", 
          @"Champaign , IL 6/2013", 
          @"Indianapolis, IN 5/2013", 
          @"Kansas City, MO 4/2013", 
          @"Houston, TX 4/2013", 
          @"Davenport, IA 4/2013", 
          @"Boynton Beach, FL 2/2013", 
          @"Kissimmee, FL 1/2013"]; 

//Third section data 
self.thirdItemsArray = @[@"Kansas City, MO 12/2012", 
          @"Anaheim, CA 11/2012", 
          @"Davenport, IA 11/2012", 
          @"St Charles, IL 10/2012", 
          @"Dallas, TX 9/2012", 
          @"Monterey, CA 8/2012", 
          @"Walworth, WI 8/2012", 
          @"Des Moines, IA 7/2012", 
          @"West Allis, WI 7/2012", 
          @"St. Charles, IL 6/2012", 
          @"St. Paul, MN 6/2012", 
          @"North Little Rock, AR 6/2012", 
          @"Indianapolis, IN 5/2012", 
          @"Houston, TX 4/2012", 
          @"Walworth, WI 4/2012", 
          @"Kansas City, MO 3/2012", 
          @"Kissimmee, FL 1/2012"]; 

//Fourth section data 
self.fourthItemsArray = @[@"Kansas City, MO 12/2011", 
          @"Dallas, TX 10/2011", 
          @"Fontana, WI 9/2011", 
          @"St. Charles, IL 9/2011", 
          @"Monterey, CA 8/2011", 
          @"Walworth, WI 8/2011", 
          @"Des Moines, IA 7/2011", 
          @"West Allis, WI 7/2011", 
          @"St. Charles, IL 6/2011", 
          @"St. Paul, MN 6/2011", 
          @"Indianapolis, IN 5/2011", 
          @"Walworth, WI 3/2011", 
          @"Kansas City, MO 3/2011", 
          @"Kissimmee, FL 1/2011"]; 

//Fifth section data 
self.fifthItemsArray = @[@"Kansas City, MO 12/2010", 
          @"Canal Winchester, OH 11/2010", 
          @"Ft. Lauderdale, FL 10/2010", 
          @"Winsted, MN 10/2010", 
          @"St. Charles, IL 9/2010", 
          @"Monterey, CA 8/2010", 
          @"Walworth, WI 8/2010", 
          @"Des Moines, IA 7/2010", 
          @"St. Charles, IL 6/2010", 
          @"St. Paul, MN 6/2010", 
          @"Indianapolis, IN 5/2010", 
          @"Kansas City, MO 4/2010", 
          @"Kissimmee, FL 1/2010"]; 

//Sixth section data 
self.sixthItemsArray = @[@"Kansas City, MO 12/2009", 
          @"Branson, MO 10/2009", 
          @"St. Charles, IL 10/2009", 
          @"Canal Winchester, OH 9/2009", 
          @"Monterey, CA 8/2009", 
          @"Des Moines, IA 7/2009", 
          @"St. Charles, IL 6/2009", 
          @"St. Paul, MN 6/2009", 
          @"Indianapolis, IN 5/2009", 
          @"Kansas City, MO 3/2009", 
          @"Kissimmee, FL 1/2009"]; 

//Seventh section data 
self.seventhItemsArray = @[@"Kansas City, MO 12/2008", 
           @"St. Charles, IL 10/2008", 
           @"Canal Winchester, OH 9/2008", 
           @"Des Moines, IA 7/2008", 
           @"St. Charles, IL 6/2008", 
           @"St. Paul, MN 6/2008", 
           @"Indianapolis, IN 5/2008", 
           @"Kissimmee, FL 1/2008"]; 

} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 7; 
} 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

if(section == 0) 
    return @"Default"; 
else if(section == 1) 
    return @"2014"; 
else if(section == 2) 
    return @"2013"; 
else if(section == 3) 
    return @"2012"; 
else if(section == 4) 
    return @"2011"; 
else if(section == 5) 
    return @"2010"; 
else if(section == 6) 
    return @"2009"; 
else if(section == 7) 
    return @"2008"; 
else 
    return 0; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section 
{ 
if(section == 0) 
    return [self.defaultItemsArray count]; 
else if(section == 1) 
    return [self.firstItemsArray count]; 
else if(section == 2) 
    return [self.secondItemsArray count]; 
else if(section == 3) 
    return [self.thirdItemsArray count]; 
else if(section == 4) 
    return [self.fourthItemsArray count]; 
else if(section == 5) 
    return [self.fifthItemsArray count]; 
else if(section == 6) 
    return [self.sixthItemsArray count]; 
else if(section == 7) 
    return [self.seventhItemsArray count]; 
else 
    return 0; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"auctionCell"]; 

if(indexPath.section == 0) 
{ 
    cell.textLabel.text = [self.defaultItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 1) 
{ 
    cell.textLabel.text = [self.firstItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 2) 
{ 
    cell.textLabel.text = [self.secondItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 3) 
{ 
    cell.textLabel.text = [self.thirdItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 4) 
{ 
    cell.textLabel.text = [self.fourthItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 5) 
{ 
    cell.textLabel.text = [self.fifthItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 6) 
{ 
    cell.textLabel.text = [self.sixthItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 7) 
{ 
    cell.textLabel.text = [self.seventhItemsArray objectAtIndex:indexPath.row]; 
} 
else{ 
    cell.textLabel.text = @"Not Found"; 
} 

return cell; 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

label = cell.textLabel.text; 
} 

-(void) prepareForSegue: (UIStoryboardSegue *)segue sender: (id)sender { 
if ([segue.identifier isEqualToString:@"completeEntry"]) { 
    self.auctionName = label; 
} 
} 

@end 
+0

你一定会问好问题... – Fattie

回答

1

您的代码不起作用的原因是因为prepareForSegue在didSelectRowAtIndexPath之前调用。当使用segues时,最好不要实现didSelectRowAtIndexPath。您可以从prepareForSegue:sender:中获取从sender参数中选择的单元格。您的代码应该是这样的,

-(void) prepareForSegue: (UIStoryboardSegue *)segue sender: (UITableViewCell *)sender { 

    if ([segue.identifier isEqualToString:@"completeEntry"]) { 
     _label = sender.textLabel.text; 
     self.auctionName = _label; 
    } 
} 

这也是通常不是一个很好的做法,从细胞中获取数据,细胞是用于显示数据,不提供,但您设置数据源的方式,使一个有点困难。而不是拥有多个数组,你可以通过拥有一个数组的单个数组来获得更清晰的代码(那么你就不需要所有这些了 - 如果是)。

+0

谢谢!完美的作品。我仍然是初学者,但我会尝试清理代码。 – JerryCrowley

0

你为什么不只是使用

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    self.auctionName = cell.textLabel.text; 
} 

?在auctionTable

摆脱prepareForSegue你不显示auctionName@property定义,但它应该被定义为

@property (copy,nonatomic) NSString *auctionName 

副本将确保你得到一个字符串的副本,而不是引用一个可能被释放的字符串。

顺便说一下,作为一种风格,auctionTable应该是AuctionTablesearchCar应该是SearchCar - 类以大写字母开头。