@synthesize with UITabBarController?
I am curious if there is a good reason I should / should not be using @synthesize for the tabBarController below, or does it not matter?
@implementation ScramAppDelegate
@synthesize window;
@synthesize tabBarController;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setTabBarController:[[UITabBarController alloc] init]];
[window addSubview:[tabBarController view]];
[window makeKeyAndVisible];
return YES;
}
-(void)dealloc {
[tabBarController release];
[self setTabBarController: nil];
[window release];
[super dealloc];
}
OR
@implementation ScramAppD开发者_开发知识库elegate
@synthesize window;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
tabBarController = [[UITabBarController alloc] init];
[window addSubview:[tabBarController view]];
[window makeKeyAndVisible];
return YES;
}
-(void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
cheers Gary
I personally don't bother with @property
s for my view controllers in my application delegate class. Mainly because the app delegate sits at the top of the view hierarchy, and it doesn't need to expose its member variables to anyone.
Another reason not to comes from the line:
tabBarController = [[UITabBarController alloc] init];
Since if tabBarController
is a @property
set to retain
, you'll double-retain the object, which is a form of memory leak (although relatively benign since it happens at the app delegate level).
If tabBarController
is a property(declared in your .h file with @property (nonatomic, retain) TabBarController *tabBarController
), and you want to automatically create getter and setter methods for it, you should use @synthesize
in your .m file. If you want to implement the getter and setter yourself, you must specify @dynamic tabBarController
.
精彩评论