首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >addTarget UIButton不工作

addTarget UIButton不工作
EN

Stack Overflow用户
提问于 2017-07-04 01:14:14
回答 1查看 338关注 0票数 0

我正在尝试以编程方式创建一个按钮。当它被按下时,我想让它调用一个函数,但这不起作用。按钮显示完美。

代码语言:javascript
复制
func createSubject(name: String, color: UIColor, fromLoad: Bool){
    createVC(name: name)
    let button : UIButton = UIButton(frame: createButtonCGRect())
    UIView .animate(withDuration: 0.5, delay: 0.1, options: .curveEaseIn, animations: ({
        button.setTitle(name, for: .normal)
        button.layer.cornerRadius = button.frame.width/2
        button.backgroundColor = color
        button.setTitle(name, for: .application)
        button.isOpaque = false
        //button.addTarget(self, action: #selector(self.presentVC(button:)), for: .touchUpInside)
        //button.addTarget(self, action: #selector(self.deleteSubject(selector:)), for: .touchUpInside)
        button.addTarget(self, action: #selector(self.addToWord), for: .touchUpInside)
        button.setTitleColor(UIColor.white, for: .normal)
        view!.addSubview(button)
    }), completion: nil)
    if (fromLoad == false){
        save().saveSubject(button: button)
        disappearConfiguration()
    }else{
        UIView .animate(withDuration: 0.5, animations: {
            self.placeAddButton()
            addButton?.isUserInteractionEnabled = true
            addButton?.alpha = 1

        })
    }
}

@objc func addToWord(selector: UIButton!){
    print("HOOOLAA")
}

谢谢大家

EN

回答 1

Stack Overflow用户

发布于 2017-07-04 02:36:10

尝试下面的代码并检查

代码语言:javascript
复制
button.addTarget(self,action:#selector(self.addToWord(_:)), forControlEvents:.TouchUpInside)

祝好运。

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

https://stackoverflow.com/questions/44890960

复制
相关文章

相似问题

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