I have four buttons all with the same connection to a single function within the code, and I can't figure out how to determine which button sent the request. ~_~
Asked
Active
Viewed 135 times
4 Answers
1
You can simply check the title of the button pressed for instance with button.currentTitle
If all buttons have the same one, you can check its restoration id

La masse
- 1,190
- 1
- 10
- 24
1
As a 3rd alternative (now) you can use the tag property and simply number the single buttons.
func buttonPressed(sender: AnyObject) -> () {
if let button = sender as? UIButton {
switch (button.tag){
case 1:
//code
case 2:
//code
case 3:
//code
case 4:
//code
}
}
}
0
func buttonPressed(sender: AnyObject) -> () {
if let button = sender as? UIButton {
println("\(button.currentTitle)")
} else {
println("sender is not a UIButton!")
}
}

Asa
- 1,466
- 9
- 27
-
also pointed out in another [thread](http://stackoverflow.com/questions/30046540/get-button-pressed-id-on-swift-via-sender/30046626#30046626) tags are a good option if the titles are different – Asa May 05 '15 at 21:28
0
You could set each UIButton
's tag
to "1" "2" "3" "4"
func buttonPressed(sender: AnyObject) {
if sender.tag == 1 {
//Code for button1
} else if sender.tag == 2 {
//Code for button2
} else if sender.tag == 3 {
//Code for button3
} else if sender.tag == 4 {
//Code for button1
}
}

Chameleon
- 1,608
- 3
- 21
- 39