首先设置UITabBarController的代理为appdelegate如下:myTabBar.delegate = self;
然后在interface后面写上代理<UITabBarControllerDelegate>
实现代理里面的- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController方法即可
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ NSLog(@"--tabbaritem.title--%@",viewController.tabBarItem.title); //这里我判断的是当前点击的tabBarItem的标题 if ([viewController.tabBarItem.title isEqualToString:@"个人"]) { //如果用户ID存在的话,说明已登陆 if (USER_ID) { return YES; } else { //跳到登录页面 HPLoginViewController *login = [[HPLoginViewController alloc] init]; //隐藏tabbar login.hidesBottomBarWhenPushed = YES; [((UINavigationController *)tabBarController.selectedViewController) pushViewController:login animated:YES]; return NO; } } else return YES;}//禁止tab多次点击-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ UIViewController *tbselect=tabBarController.selectedViewController; if([tbselect isEqual:viewController]){ return NO; } return YES;}新闻热点
疑难解答