//弹出界面全部消失
+ (void)wj_AlldismissIsFinish:(void(^)(void))finish {
UIViewController *currentVC = [self wj_findVisibleViewController];
UIViewController *vcPresentVC = currentVC.presentingViewController;
if (vcPresentVC) {
while (vcPresentVC.presentingViewController) {
vcPresentVC = vcPresentVC.presentingViewController;
}
[vcPresentVC dismissViewControllerAnimated:YES completion:^{
UIViewController *resultVC = [self wj_findVisibleViewController];
if (resultVC.navigationController) {
if (resultVC.navigationController.viewControllers.count > 1) {
[resultVC.navigationController popToRootViewControllerAnimated:YES];
if (finish) {
finish();
}
}else {
if (finish) {
finish();
}
}
}else {
if (finish) {
finish();
}
}
}];
}
}
//获取根控制器
+ (UIViewController *)wj_getRootViewController {
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
NSAssert(window, @"The window is empty");
return window.rootViewController;
}
//找到最顶层控制器
+ (UIViewController *)wj_findVisibleViewController {
UIViewController* currentViewController = [self wj_getRootViewController];
BOOL runLoopFind = YES;
while (runLoopFind) {
if ([currentViewController isKindOfClass:[UINavigationController class]]) {
currentViewController = ((UINavigationController *)currentViewController).visibleViewController;
} else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
currentViewController = ((UITabBarController* )currentViewController).selectedViewController;
} else if ([currentViewController isKindOfClass:[UISplitViewController class]]) { // 当需要兼容 Ipad 时
currentViewController = currentViewController.presentedViewController;
} else {
if (currentViewController.presentedViewController) {
currentViewController = currentViewController.presentedViewController;
NSLog(@"%@",[currentViewController class]);
} else {
return currentViewController;
}
}
}
return currentViewController;
}
网友评论