1. 程式人生 > >iOS之獲取當前view的控制器、當前應用的tabbarController、navigationController中指定viewController

iOS之獲取當前view的控制器、當前應用的tabbarController、navigationController中指定viewController

-------------獲取當前view的控制器--------------

@interface UIView (CurrentController)

-(UIViewController *)getCurrentViewController;

@end

#import "UIView+CurrentController.h"

@implementation UIView (CurrentController)

-(UIViewController *)getCurrentViewController{

    UIResponder *next = [self nextResponder];

do {if ([next isKindOfClass:[UIViewController class]]) {

return (UIViewController *)next;

        }

        next = [next nextResponder];

    } while (next !=nil);

    returnnil;

}

@end

-----------------獲取當前應用的tabbarController-----------------

在APPdelegate中獲取tabbarcontroller;

LYMainTabBarController *mt=(LYMainTabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;   NSLog(@"---在APP的類gate中獲取tabbarController---%@",mt.viewControllers[2].tabBarItem.title);</span>  

在控制器中獲取tabbarController的viewcontroler,

[self.navigationController.tabBarController.viewControllers[2].tabBarItemsetBadgeValue:@"1"];


-----------獲取navigationController層級中的viewcongtroller---------------

方法一:

for (UIViewController *controllerinself.navigationController.viewControllers) {

if ([controllerisKindOfClass

:[GetMoneyVCclass]]) {

GetMoneyVC *revise =(GetMoneyVC *)controller;

            [self.navigationControllerpopToViewController:reviseanimated:YES];

        }

    }

方法二:

rootViewcontroller是陣列中的第0個元素;下一級就是第一個元素,一次類推

UIViewController *viewCtl = self.navigationController.viewControllers[1];

    [self.navigationControllerpopToViewController:viewCtl animated:YES];