iOS 开发之 点击tabbarItem添加是否跳转登录页面判断
iOS 开发之 点击tabbarItem添加是否跳转登录页面判断
项目里面在点击tabbarItem的时候需要判断是否是登录状态,如果未登录直接跳转登录页面,如果已登录则直接进入。
-
首先设置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; }
The one who wants to wear a crown must bear the weight!