我是 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/