简体   繁体   中英

Segue from UITableViewCell to ViewController not working

I have a segue set up between a UITableViewCell and a UIViewController . My understanding is that if I set up a Show segue from the prototype cell to a UIViewController , the UIViewController will be shown when tapping on one of the cells in my UITableView , but that's not happening for me. The only way to get it to work is to override the tableView didSelectRowAtIndexPath , and then call performSegueWithIdentifier .

To test this out, I threw together a quick project with a UITableViewController and a UIViewController . When I created a segue between the prototype cell and the UIViewController , it worked the way it should. So I know that it is possible, I'm just not sure what could be causing the problems in my actual project.

There's not much code to show for this. My prototype cell is a custom cell that comes from an xib file, but I also tried giving it a class of UITableViewCell , and it made no difference.

Has anyone ever run in to a case where a segue like this should work, but doesn't? If so, how did you fix it?

Let's clearify some essential things

  1. performSegueWithIdentifier: sender: is a method of UIViewController, so in order to perform segue from cell you need to declare weak property of your VC in cell, like this

@property (nonatomic, weak) UIViewController *segueController;

end perform you action via

[self.segueController performSegueWithIdentifier:@"segue_id" sender:self];
  1. The most common approach - using didSelectRowAtIndexPath has the following benefits: your segue logic belongs to controller , not to the view (because uitableviewcell is a child of uiview). so, i recommend you use this method for segues or manipulating with view hierarchy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM