Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBOutlet declarations?

I have seen the code below written 3 different ways (with regards to IBOutlet) Does it matter, I would say adding IBOutlet to both the declaration and the @property was more concise.

JUST PROPERTY:

@class SwitchViewController;

@interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    SwitchViewController *switchViewController;
}

@property(nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
@end

JUST DECLARATION:

@class SwitchViewController;

@interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
    IBOutlet UIWindow *window;
    IBOutlet SwitchViewController *switchViewController;
}

@property(nonatomic, retain) UIWindow *window;
@property(nonatomic, retain) SwitchViewController *switchViewController;
@end

BOTH:

@class SwitchViewController;

@interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
    IBOutlet UIWindow *window;
    IBOutlet SwitchViewController *switchViewController;
}

@property(nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
@end

cheers gary

like image 696
fuzzygoat Avatar asked Feb 05 '10 11:02

fuzzygoat


2 Answers

IBOutlet does only matter to InterfaceBuilder. For the compiler, UINibDeclarations.h #defines it to nothing.

InterfaceBuilder takes IBOutlet as a hint from the header file to list the available outlets for the class. If you wire up an object to an IBOutlet, no matter whether it is defined as a property or instance variable, this information is written into the nib.

When loading the nib, the loader tries to find the best possible way to setup the connection: First it tries to find a setter method with an appropriate name. If no such setter is found, it falls back to setting the instance variable directly, which is poor style, because memory management is not clear this way.

All your proposed examples have a property (and, of course, a setter method) of the right name. So in each case, the loader would use the setter method, no matter where the IBOutlet tag stands: There’s no difference between your examples, neither in the nib, nor in the way code is executed.

The best style would be to put the IBOutlet tag into the property definition.

like image 153
Nikolai Ruhe Avatar answered Oct 07 '22 02:10

Nikolai Ruhe


Should not matter. With the 10.6 64-bit SDK you can also write the property without the ivar:

@class SwitchViewController;

@interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
}

@property(nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
@end
like image 29
diederikh Avatar answered Oct 07 '22 00:10

diederikh