简体   繁体   中英

How to segue to one of several subtypes of UIViewController with a storyboard?

I have a table whose detail-disclosure buttons bring up a MyDetailViewController and in performSegueWithIdentifier:sender: I have been setting a few variables. But it turns out that I need something more complex: I need subtypes of MyDetailViewController ( MyFooController : MyDetailViewController , MyBarController : MyDetailViewController , etc.) based on what row is selected.

My first thought was to assign to the destinationViewController in the UIStoryboardSegue in performSegueWithIdentifer:sender: but that is a read-only variable.

Is there an easy way to do this with Storyboards? Should I create a custom Segue? Or should I just do it programmatically?

You can either use multiple segues, one for each of your subtypes of controllers, there is no actual limit to how many you can use from one viewController, or you can, depending on the cell that was selected, instantiate a view controller, and push it on the stack manually, something in the lines of:

MyBarController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"myBarController"]; // You set the identifiers in the storyboard itself
[self.navigationController pushViewController:vc animated:YES];

You can't connect multiple segues to a detail-disclosure button, but you can connect as many segues as you want from the controller. Implement tableView:accessoryButtonTappedForRowWithIndexPath:, and that method will be called when a user taps the button. You can use the indexPath argument to decide which segue to execute, and then invoke the segue using performSegueWithIdentifier.

As a third alternative, you could create different prototype cells in the storyboard, give each a different reuseIdentifier , and create the different segues from each prototype cell. Then in your cellForRowAtIndexPath: method, select the correct reuseIdentifier when dequeue-ing cells, according to the row.

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