首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >飞驰UIButton addTarget自动发射

飞驰UIButton addTarget自动发射
EN

Stack Overflow用户
提问于 2015-12-16 16:48:14
回答 2查看 3.9K关注 0票数 0

我的addTarget in UIButton有个小问题。当我运行我的应用程序时,我的函数会自动启动,而他必须按下UIButton才能启动。我不知道我的问题在哪里。

我的按钮

代码语言:javascript
复制
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)

我的职能

代码语言:javascript
复制
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)")
    }

预先感谢您的答复。

EN

回答 2

Stack Overflow用户

发布于 2015-12-16 17:04:16

你不能用这种方式添加一个选择器。你需要像这样改变它:

代码语言:javascript
复制
myBTN.addTarget(self, action: Selector("data:"), forControlEvents: .TouchUpInside)

还将方法签名更改为:

代码语言:javascript
复制
func data(sender: UIButton)
{
   NSLog("Button clicked")
}
票数 1
EN

Stack Overflow用户

发布于 2015-12-16 17:20:11

为了解决我的问题,我在UIButton中使用了标记。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34317576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档