报告来自子ViewController的更改

时间:2016-12-29 03:53:36

标签: swift uiviewcontroller

如果一个ViewController继承自另一个ViewController,那么当父ViewController中的变量发生变化时,如何更新子ViewController中的东西?

class ViewControllerOne: UIViewController {

    var timer = Timer()

    var number: Int = 0  

    func updateNumber() {
        number += 1
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateNumber), userInfo: nil, repeats: true)
    }
}



class ViewControllerTwo: ViewControllerOne {
    // So in this class I want to print to the console when number is 3.
    // How do I check for that from this class?
}

1 个答案:

答案 0 :(得分:1)

覆盖updateNumber中的ViewControllerTwo

override func updateNumber() {
    super.updateNumber()

    if number == 3 {
        // do something
    }
}