今天做項(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ò)。


兩個(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è)";

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