Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vonbaron

macrumors newbie
Original poster
Nov 8, 2011
5
0
I am working on an app where I load data into a UITableView based on results from an SQLite query.

Here is how I populate the table view:

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ObjectCell"];
    
    catObj = [resultsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = catObj.object;
    cell.detailTextLabel.text = catObj.con;
    
    return cell;
}

When a cell in the table view is selected I segue to a UIView. Within the UIView I want to display the details of the catObj that was selected in the table view.

To capture the selected object I use this method:

Code:
- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    selectedObject = [resultsArray objectAtIndex:[indexPath row]];
    NSLog(@"%@ is the selected object.",selectedObject.object);
  
}

Then I pass the object over to the DetailViewController using the following method:

Code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue 
                sender:(id)sender
{

    if ([[segue identifier] isEqualToString:@"DetailViewControllerSegue"]) {
        NSLog(@"preparing segue with %@...", selectedObject.object);
        DetailViewController *dvc = (DetailViewController *)[segue destinationViewController];
       [dvc setSelection:selectedObject];
    }
    
}

Within the DetailViewController I synthesize the selection property:

Code:
@property (strong, nonatomic) CatalogObject *selection;
...
@synthesize selection;

I then attempt to populate the text fields in DetailViewController using the following method:

Code:
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    idLabel.text = selection.object;
    messLabel.text = nil;
    conLabel.text = selection.con;
    typeLabel.text = selection.type;
    magLabel.text = selection.mag;
    u2kLabel.text = selection.u2k;
    sa2kLabel.text = selection.ti;
    
}

What is happening in practice is that the program crashes when I set the selection property in the prepareForSegue method because the property is null. So it appears that the prepareForSegue method is executed before the tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath is executed.

Is there another method I should be using to either collect the object I want to pass or is there an alternate to prepareForSegue that I should use?
 
Last edited:
I have resolved this issue.

The answer is to set the label properties in the - (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath method as such:

Code:
- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    selectedObject = [resultsArray objectAtIndex:[indexPath row]];
    NSLog(@"%@ is the selected object.",selectedObject.object);
    DetailViewController *dvc = [[self storyboard] instantiateViewControllerWithIdentifier:@"DetailView"];
    

    [self.navigationController pushViewController:dvc animated:YES];  
    
    dvc.idLabel.text = selectedObject.object;

This makes the selected property and the viewDidLoad method in DetailViewContoller unnecessary as well as the prepareForSegue method.
 
I can't remember where I read it, but it is recommended to not set label's texts (and things such as this) directly but rather to assign the value to a property and then leave it to the new view controller to determine what to do with it. I believe it helps maintain the MVC approach to coding.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.