在我的FirstViewController中,我有一个按钮指向我的SecondViewController,将数据传递给SecondViewController中的一个属性。该属性具有属性观察器,在设置时创建SecondViewController的新实例。为什么在didSet中没有无限循环?
虽然它正在按照我的要求工作,但我不知道为什么它没有陷入无限循环,永远创建SecondViewController的实例。这样做是否很好?
FirstViewController:
class FirstViewController: UIViewController {
@IBAction func something(sender: UIButton) {
let destination = storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as SecondViewController
destination.selected = 1
showViewController(destination, sender: self)
}
}
SecondViewController:
class SecondViewController: UIViewController {
var selected: Int = 0 {
didSet {
let destination = storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as SecondViewController
destination.selected = selected
showViewController(destination, sender: self)
}
}
@IBAction func something(sender: UIButton) {
selected = 2
}
}
直觉上,它应该是,它用来产生一个无限循环,但在最近Swift Apple的“固定”变化中。 – 2015-03-31 07:52:03
**这是一个bug **。点击这里:http://stackoverflow.com/questions/42144752/inner-didset-protection-bizarrely-extends-to-the-whole-class – Fattie 2017-05-02 10:59:03