Vote count:
0
In my FirstViewController I have a button directing to my SecondViewController, passing data to a property in the SecondViewController. This property has a property observer, creating a new instance of the SecondViewController when set.
While it's working as I want, I wonder why it's not getting stuck in an infinite loop, creating an instance of the SecondViewController forever. And is it good practice to do it this way?
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
}
}
asked 40 secs ago
Why no Infinite loop in didSet?
Aucun commentaire:
Enregistrer un commentaire