Get the current view controller from the app delegate

前端 未结 11 1949
悲哀的现实
悲哀的现实 2020-11-30 22:46

i am new to ios. I need to know the current view controller from app delegate.. i have no idea about this and i don\'t knowto implement this. i am using this code toimplemn

相关标签:
11条回答
  • 2020-11-30 23:00

    Swift version of jjv360's great answer, (I got rid of some redundant returns, and I think Swift is more readable)

    func getCurrentViewController(_ vc: UIViewController) -> UIViewController? {
        if let pvc = vc.presentedViewController {
            return getCurrentViewController(pvc)
        }
        else if let svc = vc as? UISplitViewController, svc.viewControllers.count > 0 {
            return getCurrentViewController(svc.viewControllers.last!)
        }
        else if let nc = vc as? UINavigationController, nc.viewControllers.count > 0 {
            return getCurrentViewController(nc.topViewController!)
        }
        else if let tbc = vc as? UITabBarController {
            if let svc = tbc.selectedViewController {
                return getCurrentViewController(svc)
            }
        }
        return vc
    }
    

    From you AppDelegate,

        guard let rvc = self.window?.rootViewController else {
            return
        }
        if let vc = getCurrentViewController(rvc) {
            // do your stuff here
        }
    
    0 讨论(0)
  • 2020-11-30 23:01

    This is the best solution that I have tried out yet

    + (UIViewController*) topMostController
    {
        UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
        while (topController.presentedViewController) {
            topController = topController.presentedViewController;
        }
    
        return topController;
    }
    
    0 讨论(0)
  • 2020-11-30 23:03

    Here're some class/static functions in swift that I keep in a Utility class and can help you:

    // Returns the most recently presented UIViewController (visible)
    class func getCurrentViewController() -> UIViewController? {
    
        // If the root view is a navigation controller, we can just return the visible ViewController
        if let navigationController = getNavigationController() {
    
            return navigationController.visibleViewController
        }
    
        // Otherwise, we must get the root UIViewController and iterate through presented views
        if let rootController = UIApplication.shared.keyWindow?.rootViewController {
    
            var currentController: UIViewController! = rootController
    
            // Each ViewController keeps track of the view it has presented, so we
            // can move from the head to the tail, which will always be the current view
            while( currentController.presentedViewController != nil ) {
    
                currentController = currentController.presentedViewController
            }
            return currentController
        }
        return nil
    }
    
    // Returns the navigation controller if it exists
    class func getNavigationController() -> UINavigationController? {
    
        if let navigationController = UIApplication.shared.keyWindow?.rootViewController  {
    
            return navigationController as? UINavigationController
        }
        return nil
    }
    
    0 讨论(0)
  • 2020-11-30 23:04

    It depends on how you set up your UI. You can possibly get your rootViewController and move through the hierarchy if it is set up in such a way.

    UIViewController *vc = self.window.rootViewController;
    
    0 讨论(0)
  • 2020-11-30 23:07

    This is what I use for finding the current view controller that the user is most likely interacting with:

    UIViewController+Utils.h

    #import <UIKit/UIKit.h>
    
    @interface UIViewController (Utils)
    
    +(UIViewController*) currentViewController;
    
    @end
    

    UIViewController+Utils.m

    #import "UIViewController+Utils.h"
    
    @implementation UIViewController (Utils)
    
    +(UIViewController*) findBestViewController:(UIViewController*)vc {
    
        if (vc.presentedViewController) {
    
            // Return presented view controller
            return [UIViewController findBestViewController:vc.presentedViewController];
    
        } else if ([vc isKindOfClass:[UISplitViewController class]]) {
    
            // Return right hand side
            UISplitViewController* svc = (UISplitViewController*) vc;
            if (svc.viewControllers.count > 0)
                return [UIViewController findBestViewController:svc.viewControllers.lastObject];
            else
                return vc;
    
        } else if ([vc isKindOfClass:[UINavigationController class]]) {
    
            // Return top view
            UINavigationController* svc = (UINavigationController*) vc;
            if (svc.viewControllers.count > 0)
                return [UIViewController findBestViewController:svc.topViewController];
            else
                return vc;
    
        } else if ([vc isKindOfClass:[UITabBarController class]]) {
    
            // Return visible view
            UITabBarController* svc = (UITabBarController*) vc;
            if (svc.viewControllers.count > 0)
                return [UIViewController findBestViewController:svc.selectedViewController];
            else
                return vc;
    
        } else {
    
            // Unknown view controller type, return last child view controller
            return vc;
    
        }
    
    }
    
    +(UIViewController*) currentViewController {
    
        // Find best view controller
        UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        return [UIViewController findBestViewController:viewController];
    
    }
    
    @end
    

    Then whenever I need the current view controller from anywhere in the app simply use:

    [UIViewController currentViewController]
    
    0 讨论(0)
  • 2020-11-30 23:09

    I found this and it works great for me for all types of segues and view controllers. It's very simple and short too which is nice.

    +(UIViewController*) getTopController{ UIViewController *topViewController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topViewController.presentedViewController) {
        topViewController = topViewController.presentedViewController;
    }
    
    return topViewController; }
    
    0 讨论(0)
提交回复
热议问题