簡體   English   中英

addTarget UIButton不起作用

[英]addTarget UIButton not working

我正在嘗試以編程方式創建一個按鈕。 當我按下它時,我希望它調用一個函數,但這是行不通的。 按鈕顯示完美。

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

謝謝你們

試試下面的代碼並檢查

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

祝好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM