Get button pressed id on Swift via sender

后端 未结 15 1132
广开言路
广开言路 2020-12-01 04:21

So I have a storyboard with 3 buttons I want to just create 1 action for all those 3 buttons and decide what to do based on their label/id...

Is there a way to get s

15条回答
  •  时光说笑
    2020-12-01 05:05

    Swift 3 Code: In xcode Please set tag for each button first to work following code.

    @IBAction func threeButtonsAction(_ sender: UIButton) {
    
            switch sender.tag {
            case 1:
                print("do something when first button is tapped")
                break
            case 2:
                print("do something when second button is tapped")
                break
            case 3:
                print("do something when third button is tapped")
                break
            default:
                break
            }
        }
    

提交回复
热议问题