【问题标题】:Should I use an individual UINavigationController per Tab Bar我应该为每个标签栏使用一个单独的 UINavigationController
【发布时间】:2012-08-30 02:23:58
【问题描述】:

根据Apple,我可以使用代码组合UINavigationControllerUITabBarController,例如

MyViewController1* vc1 = [[MyViewController1 alloc] init];
MyViewController2* vc2 = [[MyViewController2 alloc] init];
MyViewController3* vc3 = [[MyViewController3 alloc] init];

MyNavRootViewController* vc4 = [[MyNavRootViewController alloc] init];
UINavigationController* navController = [[UINavigationController alloc]
                        initWithRootViewController:vc4];

NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, vc3, navController, nil];
tabBarController.viewControllers = controllers;

在这个设置中,只有 vc4UINavigationController,但是如果我想要 vc1-vc3 也有 UINavigationController 怎么办?,我应该喜欢。 .

MyViewController1* vc1 = [[MyViewController1 alloc] init];
UINavigationController* nv1 = [[UINavigationController alloc]
                        initWithRootViewController:vc1];

MyViewController1* vc2 = [[MyViewController2 alloc] init];
UINavigationController* nv2= [[UINavigationController alloc]
                        initWithRootViewController:vc2];

MyViewController1* vc3 = [[MyViewController3 alloc] init];
UINavigationController* nv3 = [[UINavigationController alloc]
                        initWithRootViewController:vc3];


NSArray* controllers = [NSArray arrayWithObjects:nv1, nv2, nv3, nil];
tabBarController.viewControllers = controllers;

这是正确的方法吗?

【问题讨论】:

  • 嗯,这取决于您是否想要...你的目标是什么?
  • 如果您必须在单个选项卡中浏览多个视图,那么您将需要使用 navigationController。如果每个选项卡要显示单个 ViewController,则不要使用 navigationController。这完全取决于您的要求。

标签: iphone objective-c ios xcode cocoa


【解决方案1】:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];


// Override point for customization after application launch.

self.tabBarController = [[[UITabBarController alloc] init] autorelease];

 self.tabBarController.viewControllers = [self initializeTabBarItems];
self.navigationController = [[UINavigationController alloc]init];
[self.navigationController setNavigationBarHidden:YES];
self.window.rootViewController = self.navigationController;
[self.navigationController pushViewController:_tabBarController animated:YES];
[self.window makeKeyAndVisible];
return YES;
}
- (NSArray *)initializeTabBarItems
{
NSArray * retval;

/* Initialize view controllers */
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
UIViewController *viewController3 = [[[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil]autorelease];
UIViewController *viewController4 = [[[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil] autorelease];
UIViewController *viewController5  = [[[FivfthViewController alloc] initWithNibName:@"FivfthViewController" bundle:nil] autorelease];


/* Initialize navigation controllers */
UINavigationController * navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController * navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController * navigationController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
UINavigationController * navigationController4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
UINavigationController * navigationController5 = [[UINavigationController alloc] initWithRootViewController:viewController5];

/*  Release View Controllers */
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
[viewController5 release];

/* Stuff Navigation Controllers into return value */
retval = [NSArray arrayWithObjects:viewController1,viewController2,viewController3,viewController4,viewController5,nil];

/* Release Navigation Controllers */
[navigationController1 release];
[navigationController2 release];
[navigationController3 release];
[navigationController4 release];
[navigationController5 release];

return (retval);
}

你可以试试这个......

【讨论】:

  • 不应该是:retval = [NSArray arrayWithObjects: navigationController1, navigationController2, navigationController3, navigationController4, navigationController5,nil];
【解决方案2】:

是的,霍华德,你的方法很好。 Apple 也这么说。在使用 UITabbarControllerUINavigationController 时,我也遵循相同的方法,这对我来说非常有用。

【讨论】:

    【解决方案3】:

    您的 TabBarController 的每个选项卡都应该有一个 UINavigationController。所以你的第二种方法是正确的。我不认为您可以为所有选项卡重用相同的导航控制器。

    【讨论】:

      【解决方案4】:

      是的,您的方法是正确的。

      如果你必须将视图导航到选项卡中,那么该选项卡应该有导航控制器。

      UINavigationController * navigationCtrl = [[UINavigationController alloc] initWithRootViewController:firstTabViewCtrl];
      [arrTabs addObject:navigationCtrl];
      

      无论哪种方式,标签内都不需要导航控制器。

       [arrTabs addObject:firstTabViewCtrl];
      

      【讨论】:

        猜你喜欢
        • 2021-02-07
        • 1970-01-01
        • 1970-01-01
        • 2022-08-11
        • 2016-01-14
        • 2018-11-16
        • 2021-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多