關(guān)于在TabbarController的viewcontrollers的NavigationBar上設(shè)置rightBarButtonItem的問(wèn)題

今天做項(xiàng)目,碰見(jiàn)了一個(gè)問(wèn)題,在tabbar的viewcontrollers中的一個(gè)頁(yè)面上加入rightBarButtonItem,結(jié)果怎么也不顯示,同樣的代碼換在其它的VC上試時(shí),就除了同樣身為tabbar的viewcontrollers的VC不顯示以外其余都正常,所以我認(rèn)為是tabbar和navigationbar的問(wèn)題,但是以前我也碰見(jiàn)過(guò)同樣的需求,也正常完成,所以我認(rèn)真的研究了這一部分的代碼。
AppDelegate中關(guān)于程序入口是這樣寫(xiě)的:

 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[TJMainViewController new]];//TJMainViewController是一個(gè)TabbarController

在TJMainViewController添加viewControllers的代碼

//初始化根視圖控制器
- (void)initRootViewController{
    
    UIViewController *tjVC =[TYKYHomeViewController new];
    tjVC.title = @"首頁(yè)";
    
    UIViewController *person = [TJOnlineWorkHome tjPersonWork];
    person.title = @"個(gè)人";
    UIViewController *enterprise =[TJOnlineWorkHome tjEnterpriseWork];
    enterprise.title = @"企業(yè)";
    
    self.viewControllers = @[tjVC,person,enterprise];
    for (UIViewController *vc in self.viewControllers) {
        vc.tabBarItem.image = [[UIImage imageNamed:@"unselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.tabBarItem.selectedImage = [[UIImage imageNamed:@"selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
}

我要在個(gè)人和企業(yè)中添加一個(gè)rightBarButtonItem,我的代碼如下:

- (void)makeRightNavigationItem{
    UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    if (Screen_width>320) {
        right.frame = CGRectMake(0, 0, 25, 25);
    }
    [right setImage:[UIImage imageNamed:@"tj_help"] forState:UIControlStateNormal];
    [right addTarget:self action:@selector(showOrHiddenMoreBtnView:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
}

以上的相關(guān)代碼都已交代,均沒(méi)有出錯(cuò)。

TabBar的一級(jí)頁(yè)面.png
TabBar的二級(jí)頁(yè)面.png

兩個(gè)頁(yè)面是同樣的代碼,我試著打印出兩個(gè)頁(yè)面的navigationItem和navigationController都是存在的,并且tabbar上的三個(gè)頁(yè)面的navigationItem都是不一樣(這很重要,不一樣的話(huà)說(shuō)明可以在各自的VC中進(jìn)行修改),但是添加的rightBarButtonItem還是不顯示。
其實(shí)問(wèn)題就在于這一句:

self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[TJMainViewController new]];//TJMainViewController是一個(gè)TabbarController

把navigationController的rootVC 設(shè)置為T(mén)abbarController,則之后的在tabbar上的viewControllers雖然有各自的navigationItem,但是卻無(wú)法操作(這樣解釋不知道正不正確,如果有誤希望可以指出),這樣一來(lái),解決的思路就很清晰了。
首先,window的rootView是tabbarController,但是此tabbarController不是navigationController的rootView,而是將三個(gè)頁(yè)面:首頁(yè)、個(gè)人、企業(yè)都各自擁有一個(gè)navigationController,問(wèn)題解決。
修改的地方:
1、AppDelegate中關(guān)于程序入口

self.window.rootViewController = [TJMainViewController new];

2、 在TJMainViewController添加viewControllers的代碼

 UIViewController *tjVC =[[UINavigationController alloc] initWithRootViewController: [TYKYHomeViewController new]];
    tjVC.title = @"首頁(yè)";
    
    UIViewController *person = [[UINavigationController alloc] initWithRootViewController: [TJOnlineWorkHome tjPersonWork]];
    person.title = @"個(gè)人";
    UIViewController *enterprise = [[UINavigationController alloc] initWithRootViewController: [TJOnlineWorkHome tjEnterpriseWork]];
    enterprise.title = @"企業(yè)";
最終顯示.png

所以,給我自己的體會(huì)是,如果有TabbarController的情況下,其子控制器來(lái)作為navigationController的rootViewController,避免出現(xiàn)問(wèn)題,養(yǎng)成好習(xí)慣。

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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