Reputation:
How to identify which button was pressed. I have 3 button actions. Now I have another button which when pressed it will identify which button from the 3 was pressed. Because first you have to click from the 3 buttons. After clicking from the 3 buttons then you will click idenfifywhichpressed button and this button will identify or print which button from the 3 was pressed.
//this button will identify
@IBAction func idenfifywhichpressed(_ sender: UIButton) {
}
The 3 buttons
@IBAction func btn1(_ sender: UIButton) {
}
@IBAction func btn2(_ sender: UIButton) {
}
@IBAction func btn3(_ sender: UIButton) {
}
Upvotes: 0
Views: 6337
Reputation: 23
I prefer this approach.
@IBOutlet weak var zeroPctButton: UIButton!
@IBOutlet weak var tenPctButton: UIButton!
@IBOutlet weak var twentyPctButton: UIButton!
@IBAction func tipChanged(_ sender: UIButton) {
switch(sender) {
case zeroPctButton:
print("0%")
break;
case tenPctButton:
print("10%")
break;
case twentyPctButton:
print("20%")
break;
default:
print(" - you fail - ")
break;
}
Upvotes: 2
Reputation: 11
This is helped for me (swift 5.0)
@IBAction func hardnessSelected(_ sender: UIButton) {
print(sender.currentTitle) // <----------
}
}
Upvotes: 1
Reputation: 82
I think you can take a variable like
var checkBtn: String?
inside 1st button change the variable value
checkBtn = "1"
inside 2nd button change the variable value
checkBtn = "2"
inside 3rd button change the variable value
checkBtn = "3"
@IBAction func idenfifywhichpressed(_ sender: UIButton) {
// just print the value of CheckBtn
print(checkBtn)
//this will give you which btn pressed right
}
Upvotes: 0
Reputation: 6393
Small improvement on @prajnaranjan-das answer: casting to an enum immediately to clean up some cruft and remove the need to implement default in the switch...
enum ButtonTag: Int {
case First
case Second
case Third
}
func buttonTapped(_ sender: UIButton) {
guard let knownSender = ButtonTag(rawValue: sender.tag) else { return }
switch knownSender {
case .First:
print("do something when first button is tapped")
case .Second:
print("do something when second button is tapped")
case .Third:
print("do something when third button is tapped")
}
}
Upvotes: 1
Reputation: 6565
Do something like this,
var index:Int = 0
Now do this, FYI, set button tags accordingly 1,2 and 3...
@IBAction func btn1(_ sender: UIButton) {
index = sender.tag
}
@IBAction func btn2(_ sender: UIButton) {
index = sender.tag
}
@IBAction func btn3(_ sender: UIButton) {
index = sender.tag
}
now this,
@IBAction func idenfifywhichpressed(_ sender: UIButton) {
if index == 1 {
//Button 1 Pressed
}else if index == 2 {
//Button 2 Pressed
} else if index == 3{
//Button 3 Pressed
}
}
FYI. You can use Switch
statement as well in idenfifywhichpressed
method.
Update. Do not create three methods for btn1, btn2 and btn3, just create one method and assign tag accordingly for simplicity.
Upvotes: 0
Reputation: 1428
Try something like this
Declare an enum
enum SelectedButtonTag: Int {
case First
case Second
case Third
}
Button Action
Connect the three button actions to this method with different tag
@IBAction func idenfifywhichpressed(sender: UIButton) {
switch sender.tag {
case SelectedButtonTag.First.rawValue:
print("do something when first button is tapped")
case SelectedButtonTag.Second.rawValue:
print("do something when second button is tapped")
case SelectedButtonTag.Third.rawValue:
print("do something when third button is tapped")
default:
print("default")
}
}
Upvotes: 2