[英]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. 我有一个表,其详细信息显示按钮调出了MyDetailViewController
并且在performSegueWithIdentifier:sender:
我一直在设置一些变量。 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. 但是事实证明,我需要更复杂的东西:根据选择的行,我需要MyDetailViewController
子类型 ( MyFooController : MyDetailViewController
, MyBarController : MyDetailViewController
等)。
My first thought was to assign to the destinationViewController
in the UIStoryboardSegue
in performSegueWithIdentifer:sender:
but that is a read-only variable. 我的第一个想法是在performSegueWithIdentifer:sender:
分配给UIStoryboardSegue
中的destinationViewController
,但这是一个只读变量。
Is there an easy way to do this with Storyboards? 有没有一个简单的方法可以通过情节提要来做到这一点? Should I create a custom Segue? 我应该创建自定义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: 您可以使用多种选择,一种用于您的控制器的每个子类型,对一个viewController可以使用的数量没有实际限制,或者可以根据所选的单元实例化一个视图控制器,然后按入手动将其放在堆栈中,如下所示:
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. 实现tableView:accessoryButtonTappedForRowWithIndexPath :,当用户点击按钮时将调用该方法。 You can use the indexPath argument to decide which segue to execute, and then invoke the segue using performSegueWithIdentifier. 您可以使用indexPath参数来确定要执行哪个segue,然后使用performSegueWithIdentifier调用该segue。
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. 作为第三种选择,你可以在故事板制作不同的原型细胞,给每一个不同的reuseIdentifier
,并创建从每个原型细胞的不同塞格斯。 Then in your cellForRowAtIndexPath:
method, select the correct reuseIdentifier
when dequeue-ing cells, according to the row. 然后,在cellForRowAtIndexPath:
方法中,根据该行,在使单元出队时选择正确的reuseIdentifier
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.