为什么我不能在Swift中的UIViewController上调用默认的super.init()? [英] Why can't I call the default super.init() on UIViewController in Swift?

查看:1504
本文介绍了为什么我不能在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/

如果您没有笔尖,请为 nil (包也是可选的)。然后,您可以在 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.

这篇关于为什么我不能在Swift中的UIViewController上调用默认的super.init()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆