[英]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.