iOSApp导航层级的设计

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

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

方法中,初始化NUIViewControllerNUINavigationController,1个UITabBarController。用UIViewController的实例初始化UINavigationController,得到的NUINavigationController的实例再添加到UITabBarControllerviewControllers中,UITabBarController的实例作为self.window.rootViewController。这样UITabBarControllerviewControllers中的每一个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中设置titlenavigationItem.leftBarButtonItemnavigationItem.rightBarButtonItem等信息,需要在- (void)viewWillAppear:(BOOL)animated;
或者- (void)viewDidAppear:(BOOL)animated;
方法中调用

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

进行设置。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容