简体   繁体   English

addTarget UIButton不起作用

[英]addTarget UIButton not working

I am trying to create a button programmatically. 我正在尝试以编程方式创建一个按钮。 When it's pressed I want it to call a function, but this isn't working. 当我按下它时,我希望它调用一个函数,但这是行不通的。 Button appears perfectly. 按钮显示完美。

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")
}

Thank you all 谢谢你们

try the below code and check 试试下面的代码并检查

button.addTarget(self,action:#selector(self.addToWord(_:)), forControlEvents:.TouchUpInside)

good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM