ios - 带有自定义 rootVievController 的主 Storyboard不会调用 vc 的 init

标签 ios objective-c

我是 iOS 编程的新手,所以不要无礼) 我创建了我的 View Controller 并将其连接到 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;
}

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

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

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

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

最佳答案

当你从 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
}

编辑:

在 Objective-C 中初始化

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

关于ios - 带有自定义 rootVievController 的主 Storyboard不会调用 vc 的 init,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43124920/

相关文章:

ios - 从 NSDictionary 对象的 NSArray,如何获得 uniques 的 NSArray

ios - 自定义 tableViewCell 标签不显示

iphone - 如何将对象转发到单独的 subview ?

ios - 使用 'hidden = YES' 谓词崩溃获取 PHAsset

ios - 当原型(prototype)单元不工作时如何使用 segue

ios - 在 swift 4.2 中发送到实例的无法识别的选择器

ios - 使用现有的自定义键盘代码 xcode

ios - 除了使用 MPMoviePlayerController 播放直播视频

ios - setEnabled 或 setUserInteractionEnabled 哪个更好?

iphone - Objective-c 异步通信 : target/action or delegation pattern?