问题
I am working with Storyboard. I am in doubt regarding the use of segues when pushing a new UItableViewController onto the navigation stack. How to do it right? No segue when the push results from a tap in a cell and just use didSelectRowAtIndexPath? Please share your opinion.
回答1:
You do not mention it at all, but judging by your mention of a segue you are using storyboarding.
When using storyboarding there is a new way to show a new view controller. By connecting a UITableViewCell using a seque to a new view controller in Interface Builder. When you do that, there's no need to use didSelectRowAtIndexPath: because the new view controller will be displayed automatically. You can however prepare your segue, and set parameters on the new view controller for example:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// assumes only one type, more extensive checking before cast to
// make it safer is needed.
MyViewController *viewController = (MyViewController *) segue.destinationViewController;
viewController.detailObject = myModelObject;
}
For any tableview cell that is not connected using a segue you can provide your own behaviour using plain old tableView:didSelectRowAtIndexPath:.
There is no correct way, just use whatever seems easiest to you.
来源:https://stackoverflow.com/questions/9191619/didselectrowatindexpath-and-segues