tfcamp
tfcamp

Reputation: 115

How to identify a specific button as a sender

I'm passing data between view controllers, and am trying to send a specific int from one view controller to the other.

Right now, my code looks like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let werrk = segue.destination as! ViewControllerTwo
    werrk.dataSent = (sender as! UIButton).title(for: .normal)!
    }

It sends the title of a button to a new view controller successfully. To also identify a specific button and send a int, I'm imagining adding some if/else statements like...

if sender = button1 {
work.dataSent = 7
}

I can't find the correct commands to make this happen however. I'm new to all of this and any help would be appreciated. Thanks very much!

Upvotes: 0

Views: 1489

Answers (2)

Lysdexia
Lysdexia

Reputation: 443

try adding a Tag on each button then use it to identify on if/switch

Button1.tag = 1

and on your event trigger do something like

if (sender.tag == 1) {
   // do stuffs
}

Upvotes: 2

Fahim
Fahim

Reputation: 3556

You'd have to add an IBOutlet for each of the buttons you want to compare in your code and then link the outlet to the actual button in your storyboard. Then you can check if the sender is that particular button.

If this does not make sense, please let me know and I'll elaborate further.

Upvotes: 1

Related Questions