我一直在尝试遵循有关创建容器视图控制器的教程.它在Objective-C中.我想把它转换成
Swift.我在这里找到了一些相同的问题,但我没有太多的东西.
这是代码.
import UIKit class ContainerViewController: UIViewController { // Class "ContainerViewController" has no initializers - That I know why. // 'required' initializer 'init(coder:)' must be provided by a subclass of UIViewController var currentDetailViewController: UIViewController override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
我曾经尝试过这两个错误都说出来,但是仍然不行.
解决方法
问题是:如果您声明任何存储的属性没有初始值,您必须实现自己的初始化程序来初始化它们.见
this document.
喜欢这个:
var currentDetailViewController: UIViewController override init(nibName nibNameOrNil: String?,bundle nibBundleOrNil: NSBundle?) { currentDetailViewController = UIViewController() super.init(nibName: nibNameOrNil,bundle: nibBundleOrNil) } convenience override init() { self.init(nibName: nil,bundle: nil) } required init(coder aDecoder: NSCoder) { currentDetailViewController = UIViewController() super.init(coder:aDecoder) }
但是,我认为这不是你想要的.
正确的解决方案取决于您初始化currentDetailViewController的位置.
如果您始终在viewDidLoad中初始化它,那么可以将其声明为“Implicitly Unwrapped Optional”
var currentDetailViewController: UIViewController! override viewDidLoad() { super.viewDidLoad() self.currentDetailViewController = DetailViewController() }
否则,如果currentDetailViewController可以为零,则应将其声明为“Optional”
var currentDetailViewController: UIViewController?