iOSApp導(dǎo)航層級(jí)的設(shè)計(jì)

打開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è)UIViewControllerN個(gè)UINavigationController,1個(gè)UITabBarController。用UIViewController的實(shí)例初始化UINavigationController,得到的N個(gè)UINavigationController的實(shí)例再添加到UITabBarControllerviewControllers中,UITabBarController的實(shí)例作為self.window.rootViewController。這樣UITabBarControllerviewControllers中的每一個(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è)置。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容