简体   繁体   中英

@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 ScramAppDelegate
@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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM