开发过程中在TabBarController中很多时候有一些登录之后才可以的操作
这个时候需要在AppDelegate中判断是否登录,试过2种方法:
第一种:
- 设置UITabBarController的代理为appdelegate如下:tabBar.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
{
//跳到登录页面
LoginViewController *login = [[LoginViewController alloc] init];
//隐藏tabbar
login.hidesBottomBarWhenPushed = YES;
[((UINavigationController *)tabBarController.selectedViewController) pushViewController:login animated:YES];
return NO;
}
}
else
return YES;
}
这种方法在进入tabBarController中跳转时没有问题,但是跳转到LoginViewController中取消登录,返回到tabBarController中这个方法就不执行了。
第二种:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([window.rootViewController isKindOfClass:NSClassFromString(@"CYXTabBarController")]) {
UITabBarController * tabVC = (UITabBarController *)window.rootViewController;
if (tabVC.selectedIndex == 3) {
if (USER_ID) {
//跳到登录页面
UIStoryboard *loginSB = [UIStoryboard storyboardWithName:@"LoginViewController" bundle:nil];
LoginViewController *loginVC = [loginSB instantiateInitialViewController];
self.window.rootViewController = loginVC;
}
return UIInterfaceOrientationMaskPortrait;//返回可以设置是否允许屏幕旋转(这里是不允许)
}
}
return UIInterfaceOrientationMaskPortrait;
}
第二种方法可以完美解决问题! 关注新平台:http://www.toutiao.com/m51416718261/
关注订阅号:从小就坏