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:
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:
Then I pass the object over to the DetailViewController using the following method:
Within the DetailViewController I synthesize the selection property:
I then attempt to populate the text fields in DetailViewController using the following method:
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?
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: