本篇文章寫寫得到推送的時候,獲得當前顯示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;

}

 

完畢

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM