本篇文章写写得到推送的时候,获得当前显示vc的方法,,ios开发如何获得当前vc

刚开始干开发,那想法就是一个,实现功能, 然而到了后期,实现功能一经没有什么太大的乐趣了,这时候编程的乐趣来自如何高效的实现功能, 下面说一说做推送的时候得到数据,不用广播而是得到当前的vc或者说的到某一个vc的对象,开放加方法去做一些数据处理,接下来直接上代码 

获得当前vc    

1.以UITabBarController这种形式为工程基本框架的

if ([userInfo[@"extras"][@"PushType"] intValue] == 4 || [userInfo[@"extras"][@"PushType"] intValue] == 5) {

        UITabBarController *main = ((UITabBarController*)self.window.rootViewController).selectedViewController;

        if ([[main.viewControllers lastObject] isKindOfClass:NSClassFromString(@"DeviceViewController")]) {//设备页面

            NSLog(@"DeviceViewController");

            DeviceViewController *deviceVC = (DeviceViewController *)[main.viewControllers lastObject];

            [deviceVC refreshTem:userInfo[@"extras"]];

            NSLog(@"%@",deviceVC.whereVC);

        }else if ([[main.viewControllers lastObject] isKindOfClass:NSClassFromString(@"DeviceDetailViewController")]) {

            DeviceDetailViewController *deviceVC = (DeviceDetailViewController *)[main.viewControllers lastObject];

            [deviceVC jpushRefreshWith:userInfo[@"extras"]];

            NSLog(@"DeviceDetailViewController");

        }

    }

2以 UISubNavigationController或者UINavigationController为框架的结构

UISubNavigationController * nav =(UISubNavigationController *) self.window.rootViewController;

    if ([[nav.viewControllers lastObject] isKindOfClass:NSClassFromString(@"MessageViewController")])

    {

        return;

    }

//当前的vc  : [nav.viewControllers lastObject] 

寻找控制器中的某个vc

for (UIViewController *vc in nav.viewControllers) {

        if ([vc isKindOfClass:[OrderNewViewController class]]) {

            OrderNewViewController *vcc = (OrderNewViewController*)vc;

            switch (alertView.tag) {

                case 0:{

                    vcc.pushMark = @"0";

                    break;

                }

                case 1:{

                    vcc.pushMark = @"1";

                    break;

                }

                case 2:{

                    vcc.pushMark = @"2";

                    break;

                }

        }

            if (![vc isEqual:[nav.viewControllers lastObject]]) {

                [nav popToViewController:vcc animated:YES];

            }else{

                [vcc refreshVIewH];

            }

            return;

        }

    }

我觉得以上这些基本够用了吧

开发类方法也就是把.m的方法像+方法那样在。h中写一下, 不过这个是-方法.

一下是流传很广的获得当前vc的方法, 虽然我没用到,贴出来希望能帮到大家

1. 

//获取当前屏幕显示的viewcontroller

- (UIViewController *)getCurrentVC

{

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

    UIViewController *topVC = appRootVC;

    if (topVC.presentedViewController) {

        topVC = topVC.presentedViewController;

    }

    

    return topVC;

}

2

- (UIViewController *)getCurrentVC

{

    UIViewController *result = nil;

    

    UIWindow * window = [[UIApplication sharedApplication] keyWindow];

    if (window.windowLevel != UIWindowLevelNormal)

    {

        NSArray *windows = [[UIApplication sharedApplication] windows];

        for(UIWindow * tmpWin in windows)

        {

            if (tmpWin.windowLevel == UIWindowLevelNormal)

            {

                window = tmpWin;

                break;

            }

        }

    }

    

    UIView *frontView = [[window subviews] objectAtIndex:0];

    id nextResponder = [frontView nextResponder];

    

    if ([nextResponder isKindOfClass:[UIViewController class]])

        result = nextResponder;

    else

        result = window.rootViewController;

    

    return result;

}

完毕

原文地址:https://www.cnblogs.com/godlovexq/p/5625691.html