问题
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?
回答1:
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]
回答2:
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.
来源:https://stackoverflow.com/questions/13475365/storyboard-segue-gets-called-before-uitableviews-didselectrow