標籤:
首先設定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;}
iOS 開發之 點擊tabbarItem添加是否跳轉登入頁面判斷