Follow @endyourif rss Facebook LinkedIn

Reset UINavigationController Reset UINavigationController

Published on Apr 6, 2009

The following is a neat little trick to reset your navigation controller when it is integrated with UITabBarController.

The following code will pop the navigation controller up when the user clicks on your tab bar item. I'll begin by showing a bit of code that needs to take place. It's going to be a quick overview of this to focus on the main function to reset the navigation controller.

We start by updating our AppDeletegate.h file with the following code:

@interface AppDelegate : UITabBarController <UIApplicationDelegate, UITabBarControllerDelegate> {
 UIWindow *window;
UITabBarController *tabBarController;
UINavigationController *navigationController;

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;

The following code creates a global variable for our tab bar and our navigation controller. It also identifies our AppDelegate as a UITabBarController anda delegate for it. This is important for our later code.

Next up we need to initialize these variables in our applicationDidFinishLaunching function:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

window = [ [ UIWindow alloc ] initWithFrame: [UIScreen mainScreen].bounds];

UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:someController];
self.navigationController = aNavigationController;
[aNavigationController release];

tabBarController = [ [ UITabBarController alloc ] init ];
tabBarController.delegate = self;
tabBarController.viewControllers = [ NSArray arrayWithObjects:self.navigationController, nil];

 // Override point for customization after application launch
[window addSubview: tabBarController.view];
 [window makeKeyAndVisible];

The following code, creates a navigation controller, it's initialized with "someController" this code assumes you've created a controller that contains a UITableView in it called "someController". Next up it allocates our tab bar and tells it what buttons to create.

Finally, it addes it our window and shows it.

Running this code should load up a window and display one tab bar button. Now, we need to implement a function that gets called whenever we click a tab bar item:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[self.navigationController popViewControllerAnimated:YES];

The above code simply "pops" our view controller. Assuming the navigationController contains a view with a table listing in it and you've navigated to the second level, when you click the tab bar item again it will reset the view to the top level. This is very handy so it doesn't always remember what item you clicked in your table view.

Basically, it's like we clicked the "Back" button that appears beside the title.

Tags: iPhone | UINavigationController | UITabBarController

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP