ios 从应用程序委托获取当前视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24825123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:26:46  来源:igfitidea点击:

Get the current view controller from the app delegate

iosobjective-cdelegatesappdelegate

提问by user3459648

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 toimplemnt this but it return null values. I followed this link- Get current view controller from the app delegate (modal is possible)need help.

我是 ios 新手。我需要从应用程序委托知道当前的视图控制器..我不知道这个,我不知道要实现这个。我正在使用此代码来实现它,但它返回空值。我按照这个链接 - 从应用程序委托获取当前视图控制器(模式是可能的)需要帮助。

回答by jjv360

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

这是我用来查找用户最有可能与之交互的当前视图控制器的方法:

UIViewController+Utils.h

UIViewController+Utils.h

#import <UIKit/UIKit.h>

@interface UIViewController (Utils)

+(UIViewController*) currentViewController;

@end

UIViewController+Utils.m

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]

回答by Alexander Miller

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

以下是我保存在 Utility 类中的一些 swift 类/静态函数,可以帮助您:

// 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
}

回答by Abdusha M A

This helped me to find the visible view controller. I searched for existing methods and didn't find any. So I wrote my own custom one.

这帮助我找到了可见的视图控制器。我搜索了现有的方法,但没有找到任何方法。所以我写了我自己的自定义。

-(id)getCurrentViewController
{
    id WindowRootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];

    id currentViewController = [self findTopViewController:WindowRootVC];

    return currentViewController;
}

-(id)findTopViewController:(id)inController
{
    /* if ur using any Customs classes, do like this.
     * Here SlideNavigationController is a subclass of UINavigationController.
     * And ensure you check the custom classes before native controllers , if u have any in your hierarchy.
    if ([inController isKindOfClass:[SlideNavigationController class]])
    {
        return [self findTopViewController:[inController visibleViewController]];
    }
    else */
    if ([inController isKindOfClass:[UITabBarController class]])
    {
        return [self findTopViewController:[inController selectedViewController]];
    }
    else if ([inController isKindOfClass:[UINavigationController class]])
    {
        return [self findTopViewController:[inController visibleViewController]];
    }
    else if ([inController isKindOfClass:[UIViewController class]])
    {
        return inController;
    }
    else
    {
        NSLog(@"Unhandled ViewController class : %@",inController);
        return nil;
    }
}

And sample use :

和示例使用:

-(void)someMethod
{
    id currentVC = [self getCurrentViewController];
        if (currentVC)
        {
            NSLog(@"currentVC :%@",currentVC);
        }
}

回答by endavid

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

jjv360 的 Swift 版本很好的答案,(我去掉了一些多余的回报,我认为 Swift 更具可读性)

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,

来自您的 AppDelegate,

    guard let rvc = self.window?.rootViewController else {
        return
    }
    if let vc = getCurrentViewController(rvc) {
        // do your stuff here
    }

回答by thomasdclark

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.

这取决于您如何设置 UI。如果以这种方式设置,您可以获取您的 rootViewController 并在层次结构中移动。

UIViewController *vc = self.window.rootViewController;

回答by Reconquistador

UIViewController* actualVC = [anyViewController.navigationController.viewControllers lastObject];

回答by Jorge Arimany

I get the root controller and then iterate through presented VC's:

我得到根控制器,然后遍历呈现的 VC:

 UIViewController *current = [UIApplication sharedApplication].keyWindow.rootViewController;

while (current.presentedViewController) {
    current = current.presentedViewController;
}
//now you can use current, for example to present an alert view controller:
[current presentViewController:alert animated:YES completion:nil];

回答by Sujay U N

|*| Get Visible View Controller from Navigation View Controller

|*| 从导航视图控制器获取可见视图控制器

let NavVccVar = UIApplication.sharedApplication().keyWindow?.rootViewController as! UINavigationController
let ShnSrnVar = NavVccVar.visibleViewController


|*| Presenting from the Visible View Controller

|*| 从可见视图控制器呈现

let NavVccVar = UIApplication.sharedApplication().keyWindow?.rootViewController as! UINavigationController
NavVccVar.visibleViewController!.presentViewController(NamVccVar, animated: true, completion: nil)

回答by Mayur Deshmukh

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;
}

回答by mdimarca

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.

我发现了这个,它适用于所有类型的 segue 和视图控制器。它也非常简单和简短,这很好。

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

while (topViewController.presentedViewController) {
    topViewController = topViewController.presentedViewController;
}

return topViewController; }

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

while (topViewController.presentedViewController) {
    topViewController = topViewController.presentedViewController;
}

return topViewController; }