nac*_*tel 4 ibaction ios swift swift2
我有一个IBAction连接到一个按钮,我想知道是否有任何方法来运行该功能,即使没有按下按钮.这就是我试过的......
注意:我在这个项目中使用swift.
//I get an error when I type this code?
self.buttonPressed()
@IBAction func buttonPressed(sender: AnyObject) {
print("Called Action")
}
Run Code Online (Sandbox Code Playgroud)
Sat*_*ito 13
使您的sender参数可选,并将nil传递给ButtonPressed.
self.ButtonPressed( nil )
@IBAction func ButtonPressed( sender: AnyObject? ) {
println("Called Action")
}
Run Code Online (Sandbox Code Playgroud)
一种方法是将按钮链接到其各自的界面构建器按钮,并在调用它时将其传递给您的功能.
@IBOutlet weak var yourButton: UIButton!
// When you call method
self.buttonPressed(yourButton)
//function must begin with small letter as per iOs naming convention
@IBAction func buttonPressed(sender: AnyObject) {
//println("Called Action"). This method has been renamed to print() in Swift 2.0
print("Called Action")
}
Run Code Online (Sandbox Code Playgroud)
或者,像这样定义你的函数,然后你就可以像以前一样调用你的方法:
@IBAction func buttonPressed(sender: AnyObject? = nil) {
print("Called Action")
}
//Call your method like this
self.buttonPressed()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21830 次 |
| 最近记录: |