IOS: one IBAction for multiple buttons?

If you're using interface builder to create the buttons, simply point them at the same IBAction in the relevant class You can then differentiate between the buttons within the IBAction method either by reading the text from the button (IBAction)buttonClicked:(id)sender { NSLog(@"Button pressed: %@", sender currentTitle); } or by setting the tag property in Xcode and reading it back via sender tag (If you use this approach, start the tags at 1, as 0 is the default and hence of little use. ).

If you're using interface builder to create the buttons, simply point them at the same IBAction in the relevant class. You can then differentiate between the buttons within the IBAction method either by reading the text from the button... - (IBAction)buttonClicked:(id)sender { NSLog(@"Button pressed: %@", sender currentTitle); } ...or by setting the tag property in Xcode and reading it back via sender tag. (If you use this approach, start the tags at 1, as 0 is the default and hence of little use.).

Set all the buttons to use that one action. Actions generally have a sender parameter, which you can use to figure out which button is calling the action. One popular way to tell the difference between buttons is to assign a different value to each button's tag property.So you might have 40 buttons with tags ranging from 1 to 40.

(0 probably isn't a great choice for a tag since that's what the default value is, and any button for which you forget to set the tag will have 0 as the tag value. ) This technique is most useful when all the buttons do approximately the same thing, like the buttons on a calculator or keyboard. If each of the buttons does something completely different, then you still end up with the equivalent of 40 methods, but you substitute your own switch statement for Objective-C's messaging system.

In that case, it's often better just to spend the time to create as many actions as you need an assign them appropriately.

Just use one IBAction and assign it to all your buttons.

Sure. Just connect all buttons to the same action method in Interface Builder. Use the method's sender argument (possibly in conjunction with the buttons' tag property) to identify which button is sending the event.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions