在项目中遇到了一个问题,就是项目的需求是在用户退到后台的时候再次进入软件要出现一个手势密码的界面,开始我是这样写的:
_openLockView = [[OpenLockViewController alloc] init];
UINavigationController *openNav = [[UINavigationController alloc] initWithRootViewController:_openLockView];
[self.window.rootViewController presentViewController:openNav animated:YES completion:nil];
这种想法就是用户退到后台的时候会调用APPDelegate中的-(void)applicationDidEnterBackground:(UIApplication *)application ,将上述的代码写到这个方法里,就能保证退到后台出现手势密码,但是问题也来了,对于push进来的页面都是没有问题的,界面时present进来的就会出现问题,退出时就不能保证出现手势密码。这个怎么解决呢?我们可以在退到后台的时候找到最上面的界面是什么界面,然后将手势密码界面present进来就能够解决了,这个最关键的就是找到最上面的界面:
- (UIViewController *)currentViewController{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *vc = window.rootViewController;
while (1) {
if ([vc isKindOfClass:[UITabBarController class]]) {
vc = ((UITabBarController *)vc).selectedViewController;
}
if ([vc isKindOfClass:[UINavigationController class]]) {
vc = ((UINavigationController *)vc).visibleViewController;
}
if (vc.presentedViewController) {
vc = vc.presentedViewController;
}else{
break;
}
}
return vc;
}
通过这样的方法,我们就可以得到最上面的界面,无论在什么情况下退到后台都能得到,将手势界面present进去。