Reset UINavigationController Reset UINavigationController

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.

Published on Apr 6, 2009

Tags: iPhone | UINavigationController

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.


Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.