How to find topmost view controller on iOS

后端 未结 30 2362
遥遥无期
遥遥无期 2020-11-22 08:40

I\'ve run into a couple of cases now where it would be convenient to be able to find the \"topmost\" view controller (the one responsible for the current view), but haven\'t

30条回答
  •  心在旅途
    2020-11-22 08:59

    This works great for finding the top viewController 1 from any root view controlle

    + (UIViewController *)topViewControllerFor:(UIViewController *)viewController
    {
        if(!viewController.presentedViewController)
            return viewController;
        return [MF5AppDelegate topViewControllerFor:viewController.presentedViewController];
    }
    
    /* View Controller for Visible View */
    
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    UIViewController *visibleViewController = [AppDelegate topViewControllerFor:app.window.rootViewController]; 
    

提交回复
热议问题