打開AppStore,各種類型App琳瑯滿目。我們最常用的導(dǎo)航就是標(biāo)簽欄導(dǎo)航了。今天所介紹的是在標(biāo)簽欄導(dǎo)航中,如何在整個(gè)UIApplication中使用一個(gè)UINavigationController的實(shí)例。
我們通常的做法就是在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中,初始化N個(gè)UIViewController,N個(gè)UINavigationController,1個(gè)UITabBarController。用UIViewController的實(shí)例初始化UINavigationController,得到的N個(gè)UINavigationController的實(shí)例再添加到UITabBarController的viewControllers中,UITabBarController的實(shí)例作為self.window.rootViewController。這樣UITabBarController的viewControllers中的每一個(gè)UIViewController都會(huì)擁有一個(gè)UINavigationController的實(shí)例,但是每個(gè)UIViewController都會(huì)擁有一個(gè)UINavigationController的實(shí)例并不是同一個(gè)。
如果你想一個(gè)UIApplication只擁有一個(gè)UINavigationController的實(shí)例,該如何做到呢?
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擁有同一個(gè)navigationController。
但是需要注意如果要在VC1, VC2, VC3中設(shè)置title,navigationItem.leftBarButtonItem,navigationItem.rightBarButtonItem等信息,需要在- (void)viewWillAppear:(BOOL)animated;
或者- (void)viewDidAppear:(BOOL)animated;
方法中調(diào)用
self.tabBarController.title = @“主頁”;
self.tabBarController.navigationItem.leftBarButtonItem = ;
self.tabBarController.navigationItem.rightBarButtonItem = ;
進(jìn)行設(shè)置。