addTarget UIButton不工作

时间:2017-07-03 17:14:14

标签: ios uibutton target

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

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

谢谢大家

1 个答案:

答案 0 :(得分:-1)

尝试以下代码并检查

button.addTarget(self,action:#selector(self.addToWord(_:)), forControlEvents:.TouchUpInside)
祝你好运。