为什么我不能在Swift中的UIViewController上调用默认的super.init()?
我没有在故事板中使用 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.
参见 http://www.bignerdranch.com/blog/real-iphone-crap-2-initwithnibnamebundle-is-the-designated-initializer-of-uiviewcontroller/
如果您没有笔尖,请为 loadView
中构建自定义视图,或者在中将子视图添加到
,与以往相同。 self.view
viewDidLoad
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.