Storyboard segue gets called before UITableView's didSelectRow

江枫思渺然 提交于 2019-12-07 06:47:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!