为什么我不能在 Swift 中调用 UIViewController 上的默认 super.init()? [英] Why can't I call the default super.init() on UIViewController in Swift?
问题描述
我没有使用故事板中的 UIViewController
并且我想要一个自定义的 init
函数,我在其中传入一些的 NSManagedObjectID
目的.我只想像在 Objective-C 中那样调用 super.init()
.像这样:
I am not using a UIViewController
from a storyboard and I want to have a custom init
function where I pass in an NSManagedObjectID
of some object. I just want to call super.init()
like I have in Objective-C. Like this:
init(objectId: NSManagedObjectID) {
super.init()
}
但我收到以下编译器错误:
But I get the following compiler error:
必须调用超类 UIViewController 的指定初始化器
Must call designated initializer of the superclass UIViewController
我可以不再这样做吗?
推荐答案
UIViewController
的指定初始化程序是 initWithNibName:bundle:
.你应该改为调用它.
The designated initialiser for UIViewController
is initWithNibName:bundle:
. You should be calling that instead.
如果您没有笔尖,请为笔尖名称传入 nil
(捆绑也是可选的).然后你可以在 loadView
中构造一个自定义视图,或者通过在 viewDidLoad
中向 self.view
添加子视图,就像你以前一样.
If you don't have a nib, pass in nil
for the nibName (bundle is optional too). Then you could construct a custom view in loadView
or by adding subviews to self.view
in viewDidLoad
, same as you used to.
这篇关于为什么我不能在 Swift 中调用 UIViewController 上的默认 super.init()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!