这个问题困惑了一阵。。。查阅资料也无果,突然灵感来了。。就试试,果断好使用,就记录下来,以分享;cheers;
本文环境: ios7.03 模拟器, arc;
xcode:5.02
如果。。。你用形如 :
uinavigationcontroller *nav1 =[[uinavigationcontroller alloc] initwithrootviewcontroller :[[uiviewcontroller alloc ] init]];
uinavigationcontroller *nav2 =[[uinavigationcontroller alloc] initwithrootviewcontroller :[[uiviewcontroller alloc ] init]];
nsarray *navAray =[nsarray arraywithobjects:nav1,nav2,nil];
uitabbarviewcontroller *rotbar =[uitabbarviewcontrolelr alloc] init];
rotbar.delegate =self;
[rotbar setviewcontrollers: navAray];
self.window.rootviewcontroller =rotbar;
然后,你在点击tabbaritem 的时候,要判断那个viewcontroller 的内容是否为空,是空值就要跳转到下一个viewcontrolelr;
一切到看起来很好;但是如果是双击tabbaritem;tabbarcontroller 有个默认属性,如果当前的视图控制器有navigationcontroller;就默认popToRootViewcontroller;
比较烦人;
在这里,用这个方式来实现:就能很好的处理这个问题。
实现tabbarcongtroller的代理:
这是在模拟器下 自定义的代码:
shuaxinviewcontroller 就是第一个vc ;
nextviewcontroller 就是第二个vc;
gotoNextviewcontroller就是挑战判断;
关键是这句代码:
[tabBarController setSelectedViewController:viewController];
你大概一下子就醒悟了.....
//另外模拟器和真机的实际,稍稍有些不同,请自己调试;
-(bool)tabbarviewcontrolelr:(uitabbarviewcontrolelr *)tabbarcontroller shouldSelectViewcontroler:(uiviewcontroller)viewcontrolelr{
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav =(UINavigationController *)viewController;
if (nav.viewControllers.count ==1) {
[(ShuaXinViewController *)(nav.viewControllers[0]) gotoNextViewController:nil];
[tabBarController setSelectedViewController:viewController];
return NO;
}else{
if (nav.viewControllers.count ==2) {
if ([nav.topViewController isKindOfClass:[nextViewController class]]) {
[tabBarController setSelectedViewController:viewController];
return NO;
}
}
}
}
return YES;
}
PS: 手敲的。。。不要在意细节,拼写,符号,请忽略