2011-04-28 106 views
2

出现在具有多于5个项目的选项卡栏中的moreViewController似乎正确对待视图控制器,但不处理导航控制器。任何人都可以解释为什么这是事实吗?iPhone标签栏moreNavigationController无法正确处理导航控制器?

下面是如何重现该问题的散文,然后在代码:

创建与6片6个UIViewControllers一个简单的应用程序。由于我有超过5个选项卡,选项卡“5”和“6”驻留在moreNavigationList中。 第6个标签包含一个按钮,按下该按钮可删除第一个标签。这将选项卡的数量减少到5,因此不再需要moreNavigationController并消失。标签“6”现在移动到标签栏的最后一个位置。一切都如预期。

现在,如果将视图控制器从标签“6”(即带有按钮的视图控制器)放到导航控制器中,事情就会中断。如果我按下按钮,选项卡“1”将从标签栏中删除,moreNavigationController消失,并且标签栏“6”现在显示在标签栏的最后一个位置。但它的内容已经消失。没有按钮,没有任何东西。

从分析视图层次结构看,似乎发生的事情是moreNavigationController从[tabBarController viewControllers]中的原始导航控制器中移除了“6”视图控制器并将其添加到其自己的堆栈中。但是当moreNavigationController消失时,它似乎不会放回去。

这是我用来重现这一个简单的基于窗口的测试程序的代码:使用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Create a tab bar with 5 regular view controllers and a navigation controller 
    self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 

    UIViewController* vc1 = [[[UIViewController alloc] init] autorelease]; vc1.title = @"1"; 
    UIViewController* vc2 = [[[UIViewController alloc] init] autorelease]; vc2.title = @"2"; 
    UIViewController* vc3 = [[[UIViewController alloc] init] autorelease]; vc3.title = @"3"; 
    UIViewController* vc4 = [[[UIViewController alloc] init] autorelease]; vc4.title = @"4"; 
    UIViewController* vc5 = [[[UIViewController alloc] init] autorelease]; vc5.title = @"5"; 
    UIViewController* vc6 = [[[UIViewController alloc] init] autorelease]; vc6.title = @"6"; 

    // Add a button that removes tab "1" when pressed to vc6 
    UIButton *moveButton = [self moveButton]; 
    [vc6.view addSubview:moveButton]; 
    vc6.view.backgroundColor = [UIColor greenColor]; 
    moveButton.center = vc6.view.center; 

    UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:vc6] autorelease]; 

    // Everything is fine if vc6 is added directly instead of inside a navigation controller 
    NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, vc3, vc4, vc5, navController, nil]; 
    tabBarController.viewControllers = controllers; 

    [self.window addSubview:tabBarController.view]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 


- (UIButton *)moveButton 
{ 
    UIButton *moveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    moveButton.frame = CGRectMake(0, 0, 150, 50); 
    [moveButton setTitle:@"Remove 1" forState:UIControlStateNormal]; 
    [moveButton addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside]; 
    return moveButton; 
} 


- (void)remove 
{ 
    // remove 1st tab bar item (this also removes moreNavigationController) 
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers]; 
    [viewControllers removeObjectAtIndex:0]; 
    [tabBarController setViewControllers:viewControllers]; 
} 

SDK 4.3

+1

您是否找到解决该问题的方法?我被困在迫使moreNavigationController弹出推控制器,以便控制器回到原来的导航堆栈...我发现UIKit的私有'UIMoreNavigationController'类有一个私有的'-_restoreOriginalNavigationController'方法,但它只是说moreNavigationController混乱导航堆栈。 – matm 2012-10-17 12:40:20

回答

0

我碰到了同样的问题,花了几个小时寻找到解决方案。事实证明,并不是真的。

问题是嵌套导航控制器。您的导航控制器被放置在更多导航控制器中,并且在取出时发生错误。作为delirus提示,您可以使用[moreNavigationController popToRootViewControllerAnimated:]获取导航控制器的子视图,并将它们放置在新的导航控制器中并进行显示。我尝试了这一点,并发现为什么这个问题从未得到解决:您的导航栏将“重叠”。

如果您在导航控制器中深入一些视图,然后将其放置在更多导航控制器中,则moreNavigationController的后退按钮将替换您的导航控制器的后退按钮。还有其他一些情况会导致导航栏冲突。

您可以使用自己的后退按钮创建第二个工具栏,或者覆盖More按钮的工作方式,但是我发现为了保持用户直观,只有两种解决方法 - 不要在导航栏中嵌入导航控制器控制器,或限制标签的数量。

我意识到这个问题是一个古老的,但我希望这个答案节省了一些麻烦。如果有人有任何问题(或其他解决方法的建议!)让我知道。

相关问题