Swift:不能用存储的属性覆盖并且需要'初始化程序" init(编码器:)'必须由UIViewController' UIViewController'的子类提供。

时间:2015-01-05 18:00:14

标签: swift xcode6 xcode6.1

我有以下代码:

import UIKit

class tabBarVC: UIViewController, UITabBarControllerDelegate {

    var tabBarController : UITabBarController // ERROR1:  Cannot override with a stored property 'tabBarController'

    override init() {
        super.init()
        self.tabBarController = UITabBarController()

    }
    override func viewDidLoad() { //ERROR2:  'required' initializer 'init(coder:)' must be provided by subclass for 'UIViewController' 
        super.viewDidLoad()
    }
}

我需要定义和初始化一个名为tabBarController的变量,该变量可以作为self.tabBarController访问。

我通过Xcode获得了两个错误:

  1. 无法使用存储的属性 tabBarController
  2. 覆盖
  3. '需'初始值设定项init(coder:)必须由UIViewController
  4. 的子类提供

    我做错了什么?

    **编辑**

    import UIKit
    
    class tabBarVC: UIViewController, UITabBarControllerDelegate {
    
        var tabBarController : UITabBarController() // Error: "Consecutive declarations on a line must be separated by ';'"
    
        override init() {
            super.init()
            self.tabBarController?.delegate = self
        }
    
        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    

2 个答案:

答案 0 :(得分:3)

由于您没有指定任何特定于tabBarController的内容,您可以这样做:

 var tabBarController = UITabBarController()

并没有在init方法中提及它。

对于错误2,您只需按照它所说的内容添加init(coder:),因为您在没有指定初始值设定项的情况下继承该类。

答案 1 :(得分:1)

只需将tabBarController重命名为eg。 myTabBarController。

关于所需的init:只需单击左边的红点,直到它建议插入其他代码并让它执行此操作。

更好的方法是使用awakeFromNib而不是init。