附加参数斯威夫特button.addTarget行动 [英] Attach parameter to button.addTarget action in Swift
本文介绍了附加参数斯威夫特button.addTarget行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图传递一个额外的参数给buttonClicked作用,但不能制定出语法应该是斯威夫特的东西。
I am trying to pass an extra parameter to the buttonClicked action, but cannot work out what the syntax should be in Swift.
button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
我的任何buttonClicked方式:
Any my buttonClicked method:
func buttonClicked(sender:UIButton)
{
println("hello")
}
任何人任何想法?
Anyone any ideas?
感谢您的帮助。
推荐答案
您无法通过自定义参数在 addTarget:
。一是替代设置了标签
根据标签按钮的财产和做的工作。
You cannot pass custom parameters in addTarget:
.One alternative is set the tag
property of button and do work based on the tag.
button.tag = 5
button.addTarget(self, action: "buttonClicked:",
forControlEvents: UIControlEvents.TouchUpInside)
或为雨燕2.2 的和更大的:
button.tag = 5
button.addTarget(self,action:#selector(buttonClicked),
forControlEvents:.TouchUpInside)
现在根据标记做逻辑
属性
func buttonClicked(sender:UIButton)
{
if(sender.tag == 5){
var abc = "argOne" //Do something for tag 5
}
println("hello")
}
这篇关于附加参数斯威夫特button.addTarget行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文