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:
[code]@interface AppDelegate : UITabBarController <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;
[/code]
The following code creates a global variable for our tab bar and our navigation controller. It also identifies our AppDelegate as a UITabBarController and a delegate for it. This is important for our later code.
Next up we need to initialize these variables in our applicationDidFinishLaunching function:
[code]- (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];
}
[/code]
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:
[code]- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[self.navigationController popViewControllerAnimated:YES];
}
[/code]
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.
Pingback: How an Internet Marketing Business Works | eportalsite.com | Science News 2009
Pingback: hunter pvp
Pingback: warlock pvp guide
Pingback: how to keep fit
Pingback: remote virus removal
Pingback: Goozle Zone
Pingback: Ania Antonette Quisumbing
Pingback: bad credit loans
Pingback: havanera guayabera
Pingback: buy songs online itunes
Pingback: onebuckresume.com
Pingback: payday loans online
Pingback: ultimate power profits