所以我的代码有效,但我收到警告:
Null passed to a callee that requires a non-null argument
显然这是因为下面一行:
MyViewController *vc = [[MyViewController alloc] initWithCoder:nil];
MyViewController
是一个 Swift
类。我已经在其中实现了所需的 initWithCoder
方法。
我的问题很简单 - 我应该将什么参数传递给 initWithCoder
来终止警告,或者我使用什么备用 init
策略?
注意:我正在初始化的 View Controller 是用 Swift 编写的。然而,我正在从 Objective-c 类初始化它。
最佳答案
你应该用不同的方式来做。如果您使用 Storyboard
:
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
如果storyboard
不同
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
或者来自xib
:
MyViewController *controller = [[MyViewController alloc] initWithNibName:nibName bundle:nil];
或者如果它完全来自代码:
MyViewController *controller = [[MyViewController alloc] init];
同时从您的代码中删除 -initWithCoder:
方法
关于ios - 在 Swift 中初始化 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30055109/