【问题标题】:ContainerView embedded in ViewController: Outlets are nilViewController 中嵌入的 ContainerView:Outlets 为 nil
【发布时间】:2016-02-28 14:13:11
【问题描述】:

当我尝试使用函数从其父视图控制器更新容器视图内容时。

更新初始 ViewDidLoad 集后。应用程序崩溃。 好像所有的奥特莱斯都变为零了

【问题讨论】:

  • 更新是什么意思?应用程序崩溃,出现什么错误? Outlets 是 nil 还是变成 nil(如果是的话,你能在崩溃前在 viewDidLoad 中打印一些东西吗?告诉我们你的 viewDidLoad 里面有什么)? Container 视图中有什么?
  • 我有一个运行良好的 Parse Query。当我有一个没有容器视图的视图控制器时,它可以工作。就 UI 而言,最好有一个容器。每次我滑动容器视图控制器时,都会调用一个更新函数,它会刷新标签和图像。

标签: ios swift uicontainerview


【解决方案1】:

您需要在容器视图中获取对视图控制器的引用,然后您应该可以访问它的所有出口。将 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"
       }
    }

}

容器视图控制器

class ContainerVC: UIViewController {

@IBOutlet weak var lblDemo: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

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

【讨论】:

  • 非常感谢!这对我有帮助:)!
猜你喜欢
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多