UINavigationController does not work in moreNavigationController UITabBarController

I use UINavigationControllers in my application, all are handled by UITabBarController . Everything works fine until my controllers get to the automatically created Advanced tab.

I reproduced the problem in a simplified example. Am I doing something wrong? I can not understand.

Thank you for your help.

  #import <UIKit / UIKit.h>

 @interface testAppDelegate: NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
 {
     UIWindow * window;
     UITabBarController * tabBarController;
 }
 @property (nonatomic, retain) IBOutlet UIWindow * window;
 @property (nonatomic, retain) IBOutlet UITabBarController * tabBarController;
 @end

 @implementation testAppDelegate
 @synthesize window, tabBarController;

 - (void) applicationDidFinishLaunching: (UIApplication *) application
 {
     tabBarController = [[UITabBarController alloc] initWithNibName: nil bundle: nil];

     UINavigationController * ctrl1 = [[[[UINavigationController alloc] initWithNibName: nil bundle: nil] autorelease];
     ctrl1.tabBarItem = [[[[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemFeatured tag: 1] autorelease];

     UINavigationController * ctrl2 = [[[[UINavigationController alloc] initWithNibName: nil bundle: nil] autorelease];
     ctrl2.tabBarItem = [[[[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemFavorites tag: 2] autorelease];

     UINavigationController * ctrl3 = [[[[UINavigationController alloc] initWithNibName: nil bundle: nil] autorelease];
     ctrl3.tabBarItem = [[[[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemBookmarks tag: 3] autorelease];

     UINavigationController * ctrl4 = [[[[UINavigationController alloc] initWithNibName: nil bundle: nil] autorelease];
     ctrl4.tabBarItem = [[[[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemContacts tag: 4] autorelease];

     // This one won't work
     UINavigationController * ctrl5 = [[[[UINavigationController alloc] initWithNibName: nil bundle: nil] autorelease];
     ctrl5.tabBarItem = [[[[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemMostRecent tag: 5] autorelease];

     // This one will work
     UIViewController * ctrl6 = [[[[UIViewController alloc] initWithNibName: nil bundle: nil] autorelease];
     ctrl6.tabBarItem = [[[[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemDownloads tag: 6] autorelease];

     tabBarController.viewControllers = [NSArray arrayWithObjects: ctrl1, ctrl2, ctrl3, ctrl4, ctrl5, ctrl6, nil];

     [window addSubview: tabBarController.view];
     [window makeKeyAndVisible];
 }

 - (void) dealloc
 {
     [tabBarController release];
     [window release];
     [super dealloc];
 }

 @end
+4
source share
3 answers

Short answer: you cannot embed navigation controllers

Longer answer: you are doing it wrong. The best way to create what you want is as follows:

 NSMutableArray *viewControllers = [NSMutableArray array]; [viewControllers addObject:[[[ConverterViewController alloc] init] autorelease]]; [viewControllers addObject:[[[UINavigationController alloc] initWithRootViewController:[[[CurrencyViewController alloc] init] autorelease]] autorelease]]; [viewControllers addObject:[[[UINavigationController alloc] initWithRootViewController:[[[HistoryViewController alloc] init] autorelease]] autorelease]]; [viewControllers addObject:[[[UINavigationController alloc] initWithRootViewController:[[[SetupViewController alloc] init] autorelease]] autorelease]]; [viewControllers addObject:[[[UINavigationController alloc] initWithRootViewController:[[[HelpViewController alloc] init] autorelease]] autorelease]]; [viewControllers addObject:[[[LinksViewController alloc] init] autorelease]]; self.viewControllers = viewControllers; self.customizableViewControllers = [viewControllers arrayByRemovingFirstObject]; @implementation HelpViewController #pragma mark - #pragma mark Initialization - (id)init { if ((self = [super initWithNibName:@"HelpView" bundle:nil]) != nil) { self.title = NSLocalizedString(@"Help", @"Help"); self.tabBarItem.image = [UIImage imageNamed:@"question.png"]; } return self; } 
+2
source

I think the problem may be that you are using navigation controllers directly to push new views. Like this:

 [ctrl4 pushViewController:next animated:true]; 

But if you are on a larger tab, another navigation controller is active. You should always get the current navigation controller using the navigationController property of the current display controller.

Thus, the navigation controllers work great inside the tab bar controller.

0
source

When you set the viewControllers property to a UITabBarController, it will automatically replace the navigation controllers on view controllers 5 further using the moreNavigationController function.

I ran into a similar problem on my tab bar. This solution should help you:

Suppress moreNavigationController in custom UITabBarController

0
source

All Articles