我在主viewController
上有一个按钮,其默认值nil
由dropDown pod关联。
在同一个viewController
上还有一个容器视图。
在首次加载期间,我从共享首选项中获取变量的默认值,并通过performSegue
将该值传递给容器视图。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "dataToContainerView"){
DispatchQueue.main.async {
var secondVC = segue.destination as! secondViewController //container viewController
secondVC.variable = self.variable
}
}
}
现在我需要通过用户选择dropdown
按钮再次传递同一个变量的值。
dropDown.selectionAction = { [unowned self] (index, item) in
self.button.setTitle(item, for: UIControlState())
self.variable = item
print(item)
self.performSegue(withIdentifier: "dataToContainerView", sender: nil)
//performing segue to resend the new value of the variable.
}
上述代码正常执行到print(item)
。
但是我在performSegue上收到以下错误。
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There are unexpected subviews in the container view. Perhaps the embed segue has already fired once or a subview was added programmatically?
我应该如何在dropDown
pod的帮助下将值传递给容器视图第二次覆盖第一个值?
更新: - 我需要变量值,以便我可以将它传递给容器viewController上的json解析器。容器viewController上的代码重新执行。
答案 0 :(得分:1)
您需要保存对嵌入式控制器的引用,以便以后再次更新。在第一个segue中执行此操作:
// Declare a local variable in your parent container:
var secondVC: secondViewController!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "dataToContainerView"){
DispatchQueue.main.async {
self.secondVC = segue.destination as! secondViewController
//container viewController
self.secondVC.variable = self.variable
}
}
}
然后,当您需要更新变量时,您可以直接引用它:
self.secondVC.variable = self.variable
self.secondVC.viewDidLoad()