UINavigationController 在 UITabBarController 的 moreNavigationController 中不起作用

发布于 2024-08-04 10:43:45 字数 2511 浏览 3 评论 0原文

我正在应用程序中处理 UINavigationControllers,所有这些都由 UITabBarController 处理。一切工作正常,直到我的控制器落入自动生成的“更多”选项卡。

我在简单的示例中重现了该问题。我做错了什么吗?我想不通。

感谢您的帮助。

#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

I'm dealing with UINavigationControllers in my application, all handled by an 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-08-11 10:43:45

简短的回答:你不能嵌套导航控制器

较长的回答:你做错了。创建你想要的东西的更好方法是这样的:

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

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;
}
悲欢浪云 2024-08-11 10:43:45

我认为问题可能是您直接使用导航控制器来推送新视图。像这样:

[ctrl4 pushViewController:next animated:true];

但是,如果您在“更多”选项卡中,则另一个导航控制器处于活动状态。您必须始终使用当前显示的视图控制器的 navigationController 属性来获取当前的导航控制器。

通过这种方式,导航控制器可以在选项卡栏控制器内正常工作。

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.

By doing it this way, navigation controllers works just fine inside a tab bar controller.

红墙和绿瓦 2024-08-11 10:43:45

当您在 UITabBarController 上设置 viewControllers 属性时,它将自动将视图控制器 5 及以上的导航控制器替换为 moreNavigationController。

我在自定义标签栏上处理了类似的问题。此解决方案应该可以帮助您:

在自定义 UITabBarController 中抑制 moreNavigationController

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.

I dealt with a similar issue on my custom tab bar. This solution should help you:

Suppress moreNavigationController in custom UITabBarController

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文