iOSApp导航层级的设计

作者: __Null | 来源:发表于2016-01-10 15:27 被阅读332次

    打开AppStore,各种类型App琳琅满目。我们最常用的导航就是标签栏导航了。今天所介绍的是在标签栏导航中,如何在整个UIApplication中使用一个UINavigationController的实例。
    我们通常的做法就是在

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    

    方法中,初始化N个UIViewController,N个UINavigationController,1个UITabBarController。用UIViewController的实例初始化UINavigationController,得到的N个UINavigationController的实例再添加到UITabBarController的viewControllers中,UITabBarController的实例作为self.window.rootViewController。这样UITabBarController的viewControllers中的每一个UIViewController都会拥有一个UINavigationController的实例,但是每个UIViewController都会拥有一个UINavigationController的实例并不是同一个。

    如果你想一个UIApplication只拥有一个UINavigationController的实例,该如何做到呢?

    UIViewController *VC1 = [[UIViewController alloc] init];
    VC1.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1]];
    
    UIViewController *VC2 = [[UIViewController alloc] init];
    VC2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2]];
    
    UIViewController *VC3 = [[UIViewController alloc] init];
    VC3.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3]];
    
    UITabBarController *tabViewController = [[UITabBarController alloc] init];
    tabViewController.viewControllers = @[VC1, VC2, VC3];
    
    UINavigationController *navigationController  = [[UINavigationController alloc] initWithRootViewController:tabViewController];
    self.window.rootViewController = navigationController; 
    

    这样就保证了tabViewController.viewControllers中viewController拥有同一个navigationController。

    但是需要注意如果要在VC1, VC2, VC3中设置title,navigationItem.leftBarButtonItem,navigationItem.rightBarButtonItem等信息,需要在

    • (void)viewWillAppear:(BOOL)animated;
      或者

    • (void)viewDidAppear:(BOOL)animated;
      方法中调用

    self.tabBarController.title = @“主页”;
    self.tabBarController.navigationItem.leftBarButtonItem = ;
    self.tabBarController.navigationItem.rightBarButtonItem = ;
    

    进行设置。

    相关文章

      网友评论

      • 0x00chen:这样做的好处是什么呢?如果每个tabbar的viewcontroller 上面的导航栏都不一样 ,这样做不是每次都要在viewwillappear viewwilldisappear 里面设置吗?
        __Null:@㴴禕斨 只有几个主页的navigationbar需要在在viewwillappear.中更新。
      • PlusNie:这样的架构设计,在开发中会遇到什么样的问题?
        __Null:@NiePlus 目前没发现什么问题。对于导航的viewcontrollers的管理比较苛刻的App,这种方式可以保证只有一个导航控制器,方便视图控制器的管理。
      • pengrain:这是一个什么样的结构?
        __Null:@pengrain 用一个UINavigationController作为window.rootViewController。而navigationController的rootViewController用UITabBarController。而tabBarController.viewControllers装的直接是UIViewController,而不是UINavigationController。需要注意的是这种方式苹果并不推荐。
      • 癫癫的恋了:说的好,但这毫无意义。View Controller Catalog for iOS里面的原文:An app that uses a tab bar controller can also use navigation controllers in one or more tabs. When combining these two types of view controller in the same user interface, the tab bar controller always acts as the wrapper for the navigation controllers.
        __Null:@癫癫的恋了 Apple确实不推荐这么做,我知道的。如果一个应用只用一个导航控制器的话,有没有别的做法呢?我说的这种虽然苹果不推荐,但是目前实践中还没发现bug。

      本文标题:iOSApp导航层级的设计

      本文链接:https://www.haomeiwen.com/subject/wieqkttx.html