Home > DeveloperSection > Blogs > iOS : IBAction of UI element

iOS : IBAction of UI element

iPhone iPhone  Ios  Objective C 
0 Comment(s)
 597  View(s)
Rate this:

iOS : IBAction of UI Element  

Previously we learn about IBOutlet element : iOS : IBOutlet of UI element


As early as 2004 (and perhaps earlier), IBAction was no longer necessary for a method to be noticed by Interface Builder. Any method with the signature -(void){name}:(id)sender would be visible in the outlets pane.


Nevertheless, many developers find it useful to still use the IBAction return type in method declarations to denote that a particular method is connected to by an action. Even projects not using Storyboards / XIBs may choose to employ IBAction to call out target/action methods.


IBActions are methods that are called when some action is taken - for example, when a button is pressed, it should call a method in your code. These methods are declared in your class's @interface (.h) file.


Naming IBAction Methods


Thanks to strong, and often compiler-enforced conventions, naming is especially important in Objective-C, so the question of how to name IBAction methods is one not taken lightly. Though there is some disagreement, the preferred convention is as follows:


·         Return type of IBAction.

·         Method name of an active verb, describing the specific action performed. Method names like didTapButton: or didPerformAction: sound more like things a delegate might be sent.

·         Required sender parameter of type id. All target / action methods will pass the sender of the action (usually the responder) to methods that take a parameter. If omitted in the method signature, things will still work.

·         Optional event parameter of type UIEvent *, named withEvent: (iOS only). In UIKit, a second UIEvent * parameter, corresponding to the touch, motion, or remote control event triggering the responder, will be passed to target / action methods accepting this second parameter. The convention is to use withEvent: in the method signature, to match the UIResponder APIs.


For example:


// YES

- (IBAction)refresh:(id)sender; 

- (IBAction)toggleVisibility:(id)sender

                   withEvent:(UIEvent *)event;


// NO

- (IBAction)peformSomeAction; 

- (IBAction)didTapButton:(id)sender;


Don't want to miss updates? Please click the below button!

Follow MindStick