简体   繁体   English

从Segue加载视图控制器时调用特定的构造函数

[英]Calling specific constructor when loading a view controller from segue

I have added a segue between two view controllers using XCode 6 interface builder. 我已经使用XCode 6接口构建器在两个视图控制器之间添加了一个序列。 However it calls the default constructor. 但是,它将调用默认构造函数。 How can I get it to call a specific constructor for the second view? 如何获取第二个视图的特定构造函数?

view 1 -> click button -> activates segue -> calls constructor of view 2 -> displays view 2 

Using a segue this is unfortunately not possible, you can't further specify how the destinationViewController should be instantiated. 使用SEGUE这是不幸的是不可能的,你不能再指定如何 destinationViewController应该被实例化。

However, instead of using the Storyboard segue, you can instantiate the UIViewController yourself and just push it manually onto the navigation stack. 但是,您可以自己实例化UIViewController ,而不必手动使用Storyboard segue,而只需手动将其推入导航堆栈即可。

- (IBAction)buttonTap
{
   ViewController2 *vc2 = <your custom constructor>;
   [self.navigationController pushViewController:vc2 animated:YES]; 
}

(note that this mimics the exact same behaviour that the push segue gives you) (请注意,这模仿了推键功能给您的完全相同的行为)

Otherwise, if you want to keep on using the Storyboard segue and your actual goal is to initialize certain properties of ViewController2 , you can implemented prepareForSegue: and set the properties there. 否则,如果您想继续使用Storyboard segue,而您的实际目标是初始化ViewController2某些属性,则可以实现prepareForSegue:并在那里设置属性。

- (void)prepareForSegue:(UIStoryboardSegue *)segue
{
   ViewController2 *vc2 = segue.destinationViewController;
   // set properties here
   vc2.prop = xyz;
}

(note the code is not tested but it should convey the main ideas, let me know if you need further explanation) (请注意,该代码未经测试,但应传达主要思想,如果需要进一步说明,请告诉我)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从segue视图控制器调用父视图控制器 - calling parent View controller from segue view controller 当从弹出窗口中选择特定操作时,将 segue 添加到另一个视图 controller - Adding a segue to another view controller when specific action is picked from a popup 是否可以从另一个情节提要中选择特定的视图控制器 - Is It possible to segue to a specific view controller from another storyboard 如何根据“收藏”标识从“收藏”视图单元中选择到特定视图控制器 - How to Segue to a specific view controller depending on the Segue identity, from a Collection view cell 从appDelegate搜索到视图控制器 - Segue to a view controller from appDelegate 从NSLocalNotification打开应用程序时,将视图锁定到视图控制器 - Segue to a view controller when app opens from a NSLocalNotification 尝试从一个视图 controller 切换到另一个视图时出错 - Error when trying to segue from one view controller to another 使用AppDelegate搜索到特定的视图控制器 - Segue to specific view controller using AppDelegate 从嵌入在容器视图中的视图控制器中查看 - Segue from the view controller embedded in a container view 使用Segue从先前的View Controller传递到另一个View Controller时,数据变为Nil - Data turns Nil when passing from previous View Controller going to another View Controller using Segue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM