简体   繁体   English

UINavigationController在UITabBarController的moreNavigationController中不起作用

[英]UINavigationController doesn't work in a the moreNavigationController of a UITabBarController

I'm dealing with UINavigationControllers in my application, all handled by an UITabBarController . 我正在处理我的应用程序中的UINavigationControllers ,它们都由UITabBarController处理。 Everything works fine until my controllers fall into the automatically generated "More" tab. 一切正常,直到我的控制器进入自动生成的“更多”标签。

I reproduced the problem in the simplistic example. 我在简单的例子中重现了这个问题。 Am I doing something wrong? 难道我做错了什么? I can't figure out. 我不知道。

Thanks 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

Short answer: you cannot nest navigation controllers 简短答案:您不能嵌套导航控制器

Longer answer: you are doing it wrong. 更长的答案:您做错了。 A better way to create what you want goes like this: 创建所需内容的更好方法如下:

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;
}

When you set the viewControllers property on the UITabBarController it will automatically replace the navigation controllers on the view controllers 5 onward with the moreNavigationController. 当您在UITabBarController上设置viewControllers属性时,它将自动使用moreNavigationController替换视图控制器5上的导航控制器。

I dealt with a similar issue on my custom tab bar. 我在自定义标签栏上处理了类似的问题。 This solution should help you: 该解决方案应帮助您:

Suppress moreNavigationController in custom UITabBarController 在自定义UITabBarController中禁止moreNavigationController

I think the problem might be that you are using the navigation controllers directly to push new views. 我认为问题可能在于您是直接使用导航控制器来推送新视图。 Like this: 像这样:

[ctrl4 pushViewController:next animated:true];

But if you are in the more tab another navigation controller is active. 但是,如果您在“更多”选项卡中,则另一个导航控制器处于活动状态。 You must always get the current navigation controller using the navigationController property of the currently showing view controller. 您必须始终使用当前显示的视图控制器的navigationController属性获取当前的导航控制器。

By doing it this way, navigation controllers works just fine inside a tab bar controller. 通过这种方式,导航控制器在选项卡栏控制器中可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITabbarController与MoreNavigationController,PoptoRootViewController问题 - UITabbarController with MoreNavigationController , PoptoRootViewController issue 无法设置UITabBarController的moreNavigationController的右键 - can't set right button of UITabBarController's moreNavigationController 自定义UITabBarController的moreNavigationController - Customizing UITabBarController's moreNavigationController UITabBarController.moreNavigationController禁用项目 - UITabBarController.moreNavigationController disable items UITabBarController和UINavigationController - UITabBarController and UINavigationController 具有UINavigationController的UITabBarController - UITabBarController with UINavigationController 编辑按钮未显示在UITabBarController的MoreNavigationController中 - Edit button not displayed in UITabBarController's MoreNavigationController 可可触摸:设置UINavigationController的标题不起作用 - Cocoa Touch: Setting UINavigationController's title doesn't work 更改UITabBarController的moreNavigationController中的“编辑”按钮颜色? - Change 'edit' Button Color from moreNavigationController of UITabBarController? 无法理解UINavigationController和UITabbarController如何在Interface Builder中工作 - Having trouble understanding how UINavigationController and UITabbarController work in Interface Builder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM