Cool Basics : IBOutlet and IBAction





Introduction

While developing iPhone applications,  one of the basic concept is IBOutlet and IBAction related to interface builder.

Basics

IBOutlet stands for Interface Builder Outlet.

An Outlet is a link from code to UI. If you want to show or hide an UI element, if you want to get the text of a textfield or enable or disable an element (or a hundred other things) you have to define an outlet of that object in the sources and link that outlet through the “interface object” to the UI element. After that you can use the outlet just like any other variable in your coding.

IBAction stands for Interface Builder Action.

IBAction – a special method triggered by user-interface objects. Interface Builder recognizes them.

In other words, we need to specify IBAction for methods that will be used in IB and IBOutlet for objects that will be used in IB.

viewcontroller.h


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UIButton *btnHelloWorld;
}

- (IBAction)pressedOnHelloWorld:(id)sender;

@end


viewcontroller.m

- (IBAction)pressedOnHelloWorld:(id)sender {
    
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                                      message:@"This is my first iPhone Application"
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
}


1 comment: