问题描述
我不确定如何在Swift中为@IBAction自动调用点击功能.
Hi I am not sure how to do auto call a click-function for an @IBAction in Swift.
说,我有一个计时器功能,当倒计时完成时,我需要以编程方式调用点击功能@IBAction,而不是要求用户单击按钮
Say, I have a timer function, when countdown is finished, I need to call the Click-function @IBAction as below programmatically instead of asking user to click the button
如何迅速做到?
@IBAction func DoSomeTask(sender: UIButton) {
- code--
}
推荐答案
您可以通过将IBAction的参数设置为Optional来更改IBAction的签名,如下所示:
You can either change the signature of the IBAction by making its parameter an Optional like this:
@IBAction func doSomeTask(sender: UIButton?) {
// code
}
然后以nil作为参数调用它:
and then call it with nil as an argument:
doSomeTask(nil)
或者您可以将IBAction用作实函数的包装器:
Or you can use the IBAction as a wrapper for the real function:
func doSomeTaskForButton() {
// ...
}
@IBAction func doSomeTask(sender: UIButton) {
doSomeTaskForButton()
}
意味着您可以在任意位置调用 doSomeTaskForButton()
.
meaning you can then call doSomeTaskForButton()
from wherever you want.
这篇关于如何自动调用@IBAction函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!