EricHolsinger.com

Icon

This is where you get the spin on Eric Holsinger.

Show the UINavigationController Root View Controller in UITabBarController

When programming on iPhone or iPad, it’s common to have a UITabBarController mixed with UINavigationController.

You will probably find yourself needing to show the Root View Controller of a Navigation Controller when you switch Tabs in the Tab Controller. In your UITabBarControllerDelegate you implement the following method.


- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController;

Showing the Root View Controller is as easy as calling popToRootViewControllerAnimated. You’ll want to check the viewController parameter to see if it is a kind of UINavigationController. If it is, cast it and then call popToRootViewControllerAnimated.


- (void) tabBarController: (UITabBarController *) tabBarController
                                        didSelectViewController: (UIViewController *) viewController {
	if ([viewController isKindOfClass:[UINavigationController class]]) {
		[(UINavigationController*)viewController popToRootViewControllerAnimated:NO];
	}
}

In this way, you won’t get warnings like the following.

'UIViewController' may not respond to '-popToRootViewControllerAnimated:' ...

That is what happens when you make the call on a UIViewController like this.


[viewController popToRootViewControllerAnimated:YES];

By using isKindOfClass to identify the kind of UIViewController, you will also be able to mix your View Controllers in your Tab Controller. The check for Class membership will ensure that your are only invoking the method on the right kinds of objects.

You can read more about Introspection in the Apple
Cocoa Fundamentals Guide.

If you enjoyed this post, make sure you subscribe to my RSS feed!

 

 

Disclosure: All of the products and services discussed on this site were purchased by myself, unless otherwise noted. Affiliate links are sometimes included in the posts. I generally don't get freebies to review or get paid to review things (outside of affiliate links). If I get it for free, I'll tell you. Material compensation does not influence my opinion and my views are my own.

 

Category: Programming

Tagged:

Viewed:3,533

Leave a Reply

Contact Information

Eric Holsinger
Saco, ME 04072
ph: 207 . 749 . 1703
eM: eric.holsinger@gmail.com
My company: Whirlidoo, LLC

View Eric Holsinger's profile on LinkedIn

Categories

My Twitter noise...

  • So, iPad3 in March. Anyone have an infographic showing how far in advance the Apple magic 8 ball predictions start getting accurate? 5 hrs ago
  • I guess my biggest beef with the new twitter is that my DM indicator is always on an pegged at 4 no matter what. Can't seem to clear it. 7 hrs ago
  • Does anyone have/use a WiFi repeater or amplifier or anything like that? My WiFi is really slow near the back of my house. #wifishadow 7 hrs ago
  • More updates...

Posting tweet...

Powered by Twitter Tools