3

I have a simple question about event handling in iOS applications... suppose you have an app with some buttons that react to the TouchUpInside event calling the same action, what is the best way within the action method to understand what is the button that triggered the event? I know that it can be easily done using the title of the button, but I think it is not the best way if you have a localized app in which button text may change (unless it is possible to reverse the localization of the title, i.e. retrieve the original string from a localized string)... is there a good practice about this topic? Should I use some other property of buttons to distinguish among different buttons?

Thank you in advance for any help.

Gianni Costanzi
  • 6,054
  • 11
  • 48
  • 74
  • 1
    Sender tag is your answer. Checkout the answer to this SO. http://stackoverflow.com/questions/5542327/how-to-define-which-button-pressed-if-they-both-have-same-ibaction – user523234 Oct 15 '11 at 19:16

2 Answers2

9

There is something called a "Tag" that you can set for UIButtons, or anything that can respond to an event for that matter. If you are using Interface Builder, click the attributes inspector for the item and select a value for the tag (integer). In your code do something like this...

...

- (IBAction)buttonReceived:(id)sender
{ 
    if ([sender tag] == 1) {
       //Do something
    }
    else if ([sender tag] == 2) {
       //Do something else
    }
}
Eric
  • 5,671
  • 5
  • 31
  • 42
  • I could create an enum like `enum { BUTTON_1, BUTTON_2, etc }` and then use BUTTON_1 and BUTTON_2 within the code... is there a way to use values like these within the tag field in the Interface Builder or does it accept only numeric values? I don't have XCode under my hands so I can't try now.. Thank you for the answer. – Gianni Costanzi Oct 15 '11 at 21:52
  • It only takes numeric values in interface builder, but u for sure could make an enum in ur code to access them... just so the code is more readable.. also thanks for accepting my answer, putting me over 50 points so I can now comment haha – Eric Oct 15 '11 at 22:38
3

In addition to the tag property, or just in case you are already using the tag for some other purpose that would mean duplicate tag values for one or more different buttons, you can always set up an IBOutlet ivar to each button you needed to check, and then in the IBAction, do something like this:

- (IBAction)buttonReceived:(UIButton *)sender
{ 
    if (sender == myButtonA) {
       // processing for button A
    }
    else if (sender == myButtonB) {
       // processing for button B
    }
}

It is a bit more work, but it can come in handy at times.

BP.
  • 10,033
  • 4
  • 34
  • 53
  • This works if I'm creating buttons programmatically, not trough Interface Builder, right? Is it possible/advisable to create an IBOutlet ivar and to connect it to a button create through interface builder? BTW I think the "tag way" is the most used, right? – Gianni Costanzi Oct 15 '11 at 21:47
  • 1
    Yes, the tag is usually the way that I do this kind of detection most of the time. I did have a situation where the tag property was being used to track something else in one of my apps, however, so I had to fall back on this method. When doing this, I usually create the button in Interface Builder and connect it to the IBOutlet, but you can also do this with programmatically created buttons. Just make sure to keep references of your buttons around, or in other words, do not release the button right after you add it as a subview in the current view. – BP. Oct 17 '11 at 17:45
  • I didn't thought about linking an IBOutlet to a button, which is something really straightforward to do :) Thanks for the hint! – Gianni Costanzi Oct 18 '11 at 06:06