获取当前正在显示的任意页面(包括AlertView),然后present到新控制器。

iOS开发中,如果需要在任意一个页面中present一个controller时,因为有可能当前正在显示的页面弹出了alertview或者已经present了一个页面,导致present到新页面失效,这时我们可以这样做:

- (UIViewController *)getPresentedViewController
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    if (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }    
    return topVC;
}

如果当前没有alertview或者present页面,那将获取到rootViewController根控制器,用根控制器去present跳转,否则将获取到当前正在present的页面,用此页面去跳转。

[[self getPresentedViewController] presentViewController:_callController animated:NO completion:nil];

你可能感兴趣的:(获取当前正在显示的任意页面(包括AlertView),然后present到新控制器。)