简体   繁体   English

ContainerView 嵌入在 ViewController 中:出口为零

[英]ContainerView embedded in ViewController: Outlets are nil

As I am trying to update a container view content from its parent view controller with a function.当我尝试使用函数从其父视图控制器更新容器视图内容时。

After updating the initial ViewDidLoad sets.更新初始 ViewDidLoad 集后。 The app crashes.应用程序崩溃。 It seems like all Outlets become nil好像所有的 Outlets 都变成了 nil

You need to get a reference to the view controller in the container view and then you should have access to all its outlets.您需要在容器视图中获得对视图控制器的引用,然后您应该可以访问其所有出口。 Assign a segue identifier to the segue to the container view controller and get a reference when the segue is called.将 segue 标识符分配给容器视图控制器的 segue,并在调用 segue 时获取引用。

For example to update a label in the container view controller from a button in the parent view controller.例如,从父视图控制器中的按钮更新容器视图控制器中的标签。

Parent view controller:父视图控制器:

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"
       }
    }

}

Container View Controller容器视图控制器

class ContainerVC: UIViewController {

@IBOutlet weak var lblDemo: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM