In my iPad application, I have a single UINavigationController and multiple viewControllers acting as tabs. I'm not using the UITabbarController since I wanted certain custom look for the tabs, so have been loading the different controllers which are subclass of a single UITableViewController that encapsulates the whole tableview and cells logic on tap of the icon buttons acting as tabs at the bottom of the screen.
Looking at the design I have implemented I don't really need a navigationController as I don't need to push/pop views [which I'm doing right now] and want a single viewController to be there at a time.
What I have done now is:
In my didFinishLaunchingWithOptions method in my appDelegate, I'm allocating my navigation controller as:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UINavigationController *navigationVC = [[UINavigationController alloc]init]; navigationVC.navigationBarHidden = YES; navigationVC.navigationBar.barStyle = UIBarStyleBlack; self.navigationController = navigationVC; [window addSubview: [self.navigationController view]]; [navigationVC release]; //set orientation as portrait self.currentOrientationType = PORTRAIT; //start with launch screen view controller [self setViewController:LAUNCH param:nil]; return YES; }
And in my method setViewController:param:, depending upon the ID passed to it, it's loading the appropriate viewController as below:
- (void)setViewController:(NSString *)ID param:(NSString *)param {
UIViewController *viewController;
if(ID == HOME) {
viewController = [[HomeScreenViewController alloc]initWithNibName:@"HomeScreenViewController" bundle:nil];
}
else if(ID == ...){
}
...
//push the specified view controller
[self setTransitionType:nil];
[[self navigationController] initWithRootViewController:viewController];
[viewController release];
}
}
What is happening is any of my viewControllers thus inited as rootViewControllers aren't getting deallocated. It seems that initing the navigationController every time for each new viewController this way is wrong as it keeps a reference to its root view controller and I'm initialising it again and again with no concern to the reference count it kept on earlier viewController.
What would be a better approach as I want only one viewController there at any point in time?