【问题标题】:value of type 'UIViewController' has no member 'addSubview' Swift2'UIViewController' 类型的值没有成员 'addSubview' Swift2
【发布时间】:2016-06-01 16:09:06
【问题描述】:

我有这个错误:

“UIViewController”类型的值没有成员“addSubview”

在这段代码中。你知道为什么吗?

override func viewDidLoad() {
    self.currentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ComponentA")
    self.currentViewController!.view.translatesAutoresizingMaskIntoConstraints = false
    self.addChildViewController(self.currentViewController!)
    self.addSubview(self.currentViewController?.view, toView: self.containerView)
    super.viewDidLoad()

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    selfUIViewController 的一个实例。它定义了viewDidLoad()addChildViewController()等方法。

    方法addSubview() 是在类UIView 上定义的。如果视图控制器为其view 属性设置了视图,则可以通过调用self.view.addSubview() 访问它。

    【讨论】:

      【解决方案2】:

      addSubview 是 UIView 的方法,而不是 UIViewController

      你必须添加一个视图:

         self.view.addSubview(YOUR_VIEW)
      

      如果你想添加一个 viewController

         addChildViewController(YOUR_CHILDCONTROLLER)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 2016-03-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多