Home > Software engineering >  How to check who sent the message inside the IBAction
How to check who sent the message inside the IBAction

Time:11-03

I want to use this IBAction func as a general func

@IBAction func test(_ sender: Any) {
}

How can I know that the sender is an UIButton?

CodePudding user response:

try this

    @IBAction func test(_ sender: Any) {
        if let button = sender as? UIButton {
            // Do something with button
        }
        
        // If you don't use the button
        if sender is UIButton {
            // Do something
        }
    }

CodePudding user response:

You can set a different tag for each button. Then test which tag as the sender to know which button is tapped.

@IBAction func test(_ sender: Any) {
    if let tag = (sender as? UIView).tag {
        switch tag {
        :/ add case for each tab value
        }
    }
}
  • Related