I have the following delegate files. My AboutViewController in MainWindow.xib loads the AboutViewController.xib and the aboutViewController outlet is connected to it. Also, navController is loaded from another ViewController.xib.
AboutViewController’s view is displayed at start up and removed after one second, so i release its view controller by setting the ivar to nil.
As the about view controller is loaded by XCode automatically, am i doing something wrong(leaking memory etc.) by setting it to nil manually myself? why or why not?
Thanks
– MyProjAppDelegate.h
#import <UIKit/UIKit.h>
@interface MyProjAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UIViewController *aboutViewController;
UINavigationController *navController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UIViewController *aboutViewController;
@property (nonatomic, retain) IBOutlet UINavigationController *navController;
@end
– MyProjAppDelegate.m
#import "MyProjAppDelegate.h"
#import "AboutViewController.h"
@implementation MyProjAppDelegate
@synthesize window;
@synthesize aboutViewController;
@synthesize navController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:aboutViewController.view];
[window makeKeyAndVisible];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showMainView) userInfo:nil repeats:NO];
}
-(void)showMainView {
[aboutViewController.view removeFromSuperview];
self.aboutViewController = nil;
[window addSubview:navController.view];
}
- (void)dealloc {
[navController release];
[aboutViewController release];
[window release];
[super dealloc];
}
@end
No, you’re not doing anything wrong, because you are using the property accessor method (
self.aboutViewController) to set it tonil; the accessor will take care of therelease. (And, you are correctly removing it from the superview before releasing it 🙂