viewController与storyboard的自定义init方法 [英] viewController custom init method with storyboard
问题描述
我无法覆盖我的Storyboard中设计的CustomViewController的初始化方法。
im having trouble overriding the initialization method for my CustomViewController thats designed in my Storyboard.
现在我正在做(在我的mainViewController中):
now im doing (in my mainViewController):
self.customViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"CustomVC"];
self.customViewController.myObject = someObject;
我在viewDidLoad(CustomViewController)
and i have in viewDidLoad (CustomViewController)
[self.label setText:self.myObject.someString];
一切正常。
但是,这是正确的方法吗?我应该在CustomViewController中添加自定义init方法(或覆盖)吗?像initWithObject:?我不知道如何调用我的自定义init方法而不是 UIStoryboard instantiateViewControllerWithIdentifier:
,并且我没有调用 init
也没有 initWithNibName
。
But, is it the correct way? Should i add a custom init method (or override) to my CustomViewController ? Like initWithObject: ? I dont know how to call my custom init method instead of UIStoryboard instantiateViewControllerWithIdentifier:
, and im not getting calls to init
nor initWithNibName
.
也许我应该使用: - (id)initWithCoder:(NSCoder * )解码器
。
请给我一些建议!
谢谢!
推荐答案
故事板中视图控制器的指定初始化程序是 -initWithCoder:
。由于故事板中的大多数视图控制器都是通过segue创建的,因此您通常会在 -prepareForSegue:sender:
期间看到状态设置。
The designated initializer for view controllers in storyboards is -initWithCoder:
. Since most view controllers from a storyboard are created via segues, you usually see state set during -prepareForSegue:sender:
.
如果您直接从故事板中实例化视图控制器,就像您提供的示例一样,那么您选择的模式是合适的。
If you're instantiating a view controller directly from the storyboard like in the example you provided, then the pattern you've selected is appropriate.
这篇关于viewController与storyboard的自定义init方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!