Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storyboard segue gets called before UITableView's didSelectRow

I have a storyboard with segue from a table cell. I want to set some properties with some data when a row gets selected so I do the following:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[ProperyManager sharedPropertyManager]setSelectedRow:[verseIds objectAtIndex:indexPath.row]];
    [[ProperyManager sharedPropertyManager]setID:[poemIDs objectAtIndex:indexPath.row]];
    [[ProperyManager sharedPropertyManager]setRowToReturn:[NSString stringWithFormat:@"%i",indexPath.row]];


}

The problem is, the view controller lifecycle methods (viewWillAppear etc.) of the destination view controller get called before the didSelectRow method above, because the segue pushes the view before the delegate method is executed.

How can I get around this?

like image 878
Mohamed Emad Hegab Avatar asked Nov 20 '12 14:11

Mohamed Emad Hegab


2 Answers

Rawkode's answer is one good solution - an alternative is that, in prepareForSegue:, you can access the selected row of the table view (the sender argument will be the table view cell, you can then do [self.tableView indexPathForCell:(UITableViewCell*)sender] to get the index path) and set up whatever you need at that point.

like image 136
jrturton Avatar answered Oct 16 '22 09:10

jrturton


Don't create the Segue from the Cell to the new VC, instead set the Segue from the old VC to the new VC and give the segue an identifier.

Then within

didSelectRowAtIndexPath you can call

[self performSegueWithIdentifier:@"Segue" sender:self]

like image 45
Rawkode Avatar answered Oct 16 '22 09:10

Rawkode