如何检查是否通过标签按下了uibutton?

时间:2016-11-24 17:13:47

标签: ios swift uibutton

SELECT

2 个答案:

答案 0 :(得分:1)

您应该添加按钮的目标,如下所示:

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

功能可以这样:

func buttonPressed(sender: UIButton) {
    if sender.tag == 15 {
        // Code here
    }
}

答案 1 :(得分:0)

你可以这样做:

func addANewButton() {
    let button = UIButton(frame: CGRect(x: 1, y: 1, width: 50, height: 50))
    button.addTarget(self, action: #selector(action(_:)), for: UIControlEvents.touchUpInside)
    button.tag = 15
    view.addSubview(button)
}

func action(_ sender: UIButton) {
    switch sender.tag {
    case 15: //Do something
    case 16: //Do something else
    //Add as many cases you want...
    default: break
}

只要按下已添加目标的按钮,就会调用函数action