嵌入在ViewController中的ContainerView:Outlets是零 [英] 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.
更新初始ViewDidLoad集后。该应用程序崩溃。
似乎所有奥特莱斯都变为零
After updating the initial ViewDidLoad sets. The app crashes. It seems like all Outlets become nil
推荐答案
您需要在容器视图中获取对视图控制器的引用然后你应该可以访问它的所有网点。将segue标识符分配给容器视图控制器的segue,并在调用segue时获取引用。
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.
例如,从父视图控制器中的按钮更新容器视图控制器中的标签。
For example to update a label in the container view controller from a button in the 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"
}
}
}
容器视图控制器
class ContainerVC: UIViewController {
@IBOutlet weak var lblDemo: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
这篇关于嵌入在ViewController中的ContainerView:Outlets是零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!