嵌入在ViewController中的ContainerView:Outlets是零

JVS*_*JVS 4 ios uicontainerview swift

我正在尝试使用函数从其父视图控制器更新容器视图内容.

更新初始ViewDidLoad集后.该应用程序崩溃.似乎所有奥特莱斯都变得零

Pet*_*odd 6

您需要在容器视图中获取对视图控制器的引用,然后您应该可以访问其所有插座.将segue标识符分配给容器视图控制器的segue,并在调用segue时获取引用.

例如,从父视图控制器中的按钮更新容器视图控制器中的标签.

父视图控制器:

import UIKit

class ViewController: UIViewController {
     var containerVC : ContainerVC!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    }

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    if (segue.identifier == "segueContainer")
        {
            containerVC = segue.destinationViewController as! ContainerVC
        }
    }


@IBAction func butUpdateContainerLabelAction(sender: AnyObject) {
    if containerVC != nil{
           containerVC.lblDemo.text = "some new text"
       }
    }

}
Run Code Online (Sandbox Code Playgroud)

容器视图控制器

class ContainerVC: UIViewController {

@IBOutlet weak var lblDemo: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
   }
}
Run Code Online (Sandbox Code Playgroud)