自定义rootVievController的主故事板不会调用vc的init

时间:2017-03-30 17:44:59

标签: ios objective-c

我是iOS编程的新手,所以不要粗鲁) 我创建了我的视图控制器并将其连接到appDelegate中的Main.storyboard。它有效。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    TestViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"TestViewController"];
    [self.window setRootViewController:vc];

    return YES;
}

在init中的TestViewController.m中将一些数据放到NSArray中,但它没有执行。

- (instancetype) init {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

    self = [storyboard instantiateViewControllerWithIdentifier:@"SMKQuizViewController"]; ...

我尝试了init和initWithNib,但两者都不起作用。 我做错了什么?

1 个答案:

答案 0 :(得分:1)

从storyboard加载ViewController时,不会调用init,而是可以将代码放入

override func awakeFromNib() {
    super.awakeFromNib()
    self.yourArray = ["ABCD","EFGH"] //initialize ur array
}

如果你一心想写init :)你可以使用

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.yourArray = ["ABCD","EFGH"] //initialize ur array
}

修改

init-Objective

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        //initialize your array here :)
    }
    return self;
}