How to Auto call an @IBAction function

前端 未结 3 1259
名媛妹妹
名媛妹妹 2021-02-19 21:17

Hi I am not sure how to do auto call a click-function for an @IBAction in Swift.

Say, I have a timer function, when countdown is finished, I need to call the Click-funct

3条回答
  •  自闭症患者
    2021-02-19 22:05

    You can either change the signature of the IBAction by making its parameter an Optional like this:

    @IBAction func doSomeTask(sender: UIButton?) {
        // code
    }
    

    and then call it with nil as an argument:

    doSomeTask(nil)
    

    Or you can use the IBAction as a wrapper for the real function:

    func doSomeTaskForButton() {
        // ...
    }
    
    @IBAction func doSomeTask(sender: UIButton) {
        doSomeTaskForButton()
    }
    

    meaning you can then call doSomeTaskForButton() from wherever you want.

提交回复
热议问题