Understanding Outlets

An outlet is a way to connect your code to an object you added to a view in your storyboard that you’ve decided will become part of your user interface at runtime. The connections between your code and its outlets are automatically reestablished every time the object referenced by the outlet is loaded.

The object containing an outlet is usually a custom controller object such as the view controller generated for you by the template. In the class declaration of that custom controller, you declare an outlet as a property with the type qualifier of IBOutlet.

Listing 9-2 shows you what the TestDriveController class declaration will look like after you’re done mucking about with it at the end of this chapter.

Listing 9-2: Outlets (And Actions) Declared

#import <UIKit/UIKit.h>

@interface TestDriveController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *car;

@property (weak, nonatomic)

IBOutlet UIButton *testDriveButton;

- (IBAction)testDrive:(id)sender;

@end

An outlet is a property that points to another object, enabling an object in an application to communicate with another object at runtime. Using an outlet, you have a reference to an object defined in the storyboard (or a nib file) that is then loaded from that storyboard file at runtime. You can make outlet connections between any objects that you add to your storyboard in Interface Builder.

For example, in Figure 9-2, you use an outlet to get the text the user typed in the Find text ...

Get iPad Application Development For Dummies, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.