简体   繁体   English

如何通过IOS Swift中的任何后台进程获取当前的viewcontroller

[英]How to get current viewcontroller through any background process in IOS Swift

How to check in which viewcontroller user currently have by any background process so that action can be taken based on the viewcontroller. 如何通过任何后台进程检查哪个viewcontroller用户当前具有,以便可以基于viewcontroller采取操作。 Currently I am using swift 2.3 目前我正在使用swift 2.3

Thanks, Kamal 谢谢,卡迈勒

Here's a swift extension of UIApplication for finding the top view controller 这是UIApplication的快速扩展,用于查找顶视图控制器

extension UIApplication
{
    class func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?
    {
        if let nav = base as? UINavigationController
        {
            let top = topViewController(nav.visibleViewController)
            return top
        }

        if let tab = base as? UITabBarController
        {
            if let selected = tab.selectedViewController
            {
                let top = topViewController(selected)
                return top
            }
        }

        if let presented = base?.presentedViewController
        {
            let top = topViewController(presented)
            return top
        }
        return base
    }
}

Invoke with 调用

let topController = UIApplication.topViewController()

You can find top view controller using below method 您可以使用以下方法找到顶视图控制器

- (UIViewController *)topViewController{
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController{
    if(rootViewController.presentedViewController == nil){
        return rootViewController;
    }
    if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
        return [self topViewController:lastViewController];
    }
    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
    return [self topViewController:presentedViewController];
}

You can create a category using this method which I've already done but on my Mac, I'm typing this on my phone sorry for any bad formatting. 您可以使用我已经完成的这种方法创建一个类别,但是在我的Mac上,我在手机上输入这个,抱歉任何不好的格式化。

After you create a category call from anywhere 从任何地方创建类别调用后

[[UIApplication sharedApplication].keyWindow.rootViewController] topViewController];

I'll add this category when I get back to my Mac 当我回到我的Mac时,我会添加这个类别

EDIT 编辑

Heres the category: 继承人的类别:

@interface UIViewController (TopViewController)
- (UIViewController *)topViewController;
@end



#import "UIViewController+TopViewController.h"

@implementation UIViewController (TopViewController)

- (UIViewController *)topViewController {
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
    if (rootViewController.presentedViewController == nil) {
        return rootViewController;
    }
    if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
        return [self topViewController:lastViewController];
    }
    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
    return [self topViewController:presentedViewController];
}

@end

Forgot to mention i've got this method from here 忘了提我从这里得到这个方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM