简体   繁体   English

UIButton子类无法分配目标-Swift 4

[英]UIButton Subclass can't assign target - Swift 4

I have looked at various other answers to similar question, and followed the advice given there, but here I am getting no errors, but the target function never runs. 我查看了类似问题的其他各种答案,并按照那里给出的建议进行了操作,但是在这里我没有遇到任何错误,但是目标函数从未运行过。

I have this UIButton subclass 我有这个UIButton子类

class RoundedButton: UIButton {

var buttonColour: UIColor = UIColor.red
var height: CGFloat = 0
var width: CGFloat = 0
var text: String = ""
var x: CGFloat = 0
var y: CGFloat = 0

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

init(frame: CGRect, colour: UIColor, height: CGFloat, text: String, width: CGFloat, x: CGFloat, y: CGFloat) {
    super.init(frame: frame)
    self.buttonColour = colour
    self.height = height
    self.text = text
    self.width = width
    self.x = x
    self.y = y
    setUpView()
}



func setUpView() {
    let roundedButton = UIButton(frame: CGRect(x: x, y: y, width: width, height: height))
    roundedButton.backgroundColor = buttonColour
    roundedButton.setTitle(text, for: .normal)
    roundedButton.setTitleColor(UIColor.white, for: .normal)
    roundedButton.layer.cornerRadius = 4
    roundedButton.addTarget(self, action: #selector(self.wasPressed(_:)), for: .touchUpInside)
    self.addSubview(roundedButton)
}



@objc func wasPressed(_ sender: UIButton) {
    print("was pressed")
}

}

And I call it like this: 我这样称呼它:

let addReview = RoundedButton(frame: CGRect(), colour: #colorLiteral(red: 0.9921568627, green: 0.6941176471, blue: 0.2, alpha: 1), height: 48, text: "Review", width: UIScreen.main.bounds.width - 16, x: 8, y: UIScreen.main.bounds.maxY - 56)
                self.view.addSubview(addReview)

However the wasPressed(_ sender: UIButton) function never prints 'was pressed'. 但是wasPressed(_ sender:UIButton)函数从不打印“被按下”。 Does anyone know why this maybe? 有人知道为什么会这样吗?

Also is it possible to add the target such that it would call a 'dynamic' target in the superclass. 也可以添加目标,以便在超类中调用“动态”目标。 Eg I can send the subclass a different target each time. 例如,我可以每次向子类发送不同的目标。

This is so that I can use this RoundedButton class to make multiple buttons. 这样一来,我可以使用RoundedButton类制作多个按钮。

You don't have to add another button inside func setUpView() as you already inside the button subclass so configure it's properties directly , all the properties you declare are already exists in the button 您不必在func setUpView()添加另一个按钮,因为您已经在按钮子类中,因此可以直接配置它的属性,您声明的所有属性已经存在于按钮中

class RoundedButton: UIButton {

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    init(frame: CGRect, colour: UIColor, text: String) {
        super.init(frame: frame)

        self.backgroundColor = colour
        self.setTitle(text, for: .normal)
        self.setTitleColor(UIColor.white, for: .normal)
        self.layer.cornerRadius = 4
        self.addTarget(self, action: #selector(self.wasPressed(_:)), for: .touchUpInside)

    }

    @objc func wasPressed(_ sender: UIButton) {
        print("was pressed")
    }

}

// //

let addReview = RoundedButton(frame: CGRect(x:8,y:UIScreen.main.bounds.maxY - 56,width: UIScreen.main.bounds.width - 16, height: 48), colour: #colorLiteral(red: 0.9921568627, green: 0.6941176471, blue: 0.2, alpha: 1), text: "Review" )
self.view.addSubview(addReview)

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

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