[英]Swift UIButton addTarget launch automattically
I have a little problem with my addTarget in UIButton . 我有我在的UIButton addTarget一个小问题。 When I run my app, my function is automatically launched, while he is necessary to press the UIButton to launch.
当我运行我的应用程序时,我的功能会自动启动,而必须按下UIButton才能启动它。 I don't know where is my problem.
我不知道我的问题在哪里。
My Button 我的按钮
let BTN_happy : UIButton = UIButton()
BTN_happy.frame = CGRectMake(0, rowHeight * 1, fullWidth, rowHeight)
BTN_happy.backgroundColor = UIColor(hexString: "#ff3b30")
BTN_happy.addTarget(self, action: Selector(data("Happy")), forControlEvents: .TouchUpInside)
self.view.addSubview(BTN_happy)
text("Happy", button: BTN_happy)
My Function 我的功能
func text(text: String, button: UIButton) {
let txt : UILabel = UILabel()
txt.text = text.uppercaseString
txt.frame = CGRectMake(20, 0, fullWidth - 40, button.bounds.size.height)
txt.textColor = UIColor(hexString: "#ffffff")
txt.font = UIFont.systemFontOfSize(30, weight: UIFontWeightHeavy)
txt.shadowOffset = CGSize(width: 2, height: 2)
txt.shadowColor = UIColor(hexString: "#000000", alpha: 0.3)
button.addSubview(txt)
}
func data(mood: String) {
NSLog("Mood: \(mood)")
}
Thank you in advance for your response. 预先感谢您的回复。
You can't add a selector like that way. 您不能以这种方式添加选择器。 You need to change it like:
您需要像这样更改它:
myBTN.addTarget(self, action: Selector("data:"), forControlEvents: .TouchUpInside)
Also change the method signature like: 还要更改方法签名,例如:
func data(sender: UIButton)
{
NSLog("Button clicked")
}
为了解决我的问题,我在UIButton中使用标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.