Sunday, December 27, 2009

General steps to define and connect a IBOutlet

In Xcode development, controller class refer to object in xib using a special kind of instance variable, outlet. Outlet are declared with a special keyword, IBOutlet.

Typically, the declaration of an outet is look like this:
@property (nonatomic, retain) IBOutlet UILabel *outlet;

As a example, the text of a Label will be changed in our controller class.

modify the header file, in form of xxxViewController.h, to add declaration of the outlet.

@interface exampleViewController : UIViewController {
 UILabel *outlet
}
@property (nonatomic, retain) IBOutlet UILabel *outlet;
- (IBAction)actionMethod:(id)sender;
@end



Modify inside definition file, xxxViewController.m, to access the object using the outlet.

@implementation exampleViewController
@synthesize outlet;
-(IBAction)actionMethod:(id)sender{
 outlet.text = @"Access using outlet";
}

and have to release it in the method dealloc


- (void)dealloc {
    [outlet release];
    [super dealloc];
}



Save your works.

Connect IBOutlet in INterface Builder:

Double click on the xxxViewController.xib in Groups & Files pane, under Resources folder, to start Interface Builder.

Drag the object you want from Library to View pane, if you haven't placed it.

Hold down the Control key and drag (or drag by right button on mouse) the File's Owner in xib to over the object in View.



Release mouse button, the available outlets will be listed, select the outlet you want to connect.



that's.