美文网首页IOS理论知识
根据自己项目获取到的当前控制器

根据自己项目获取到的当前控制器

作者: IceWall_Rin | 来源:发表于2017-01-04 12:01 被阅读7次

    + (UIViewController *)getCurView {

    UIViewController *currentViewController = 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]])

    currentViewController = nextResponder;

    else

    currentViewController = window.rootViewController;

    while ([currentViewController presentedViewController]) {

    currentViewController = [currentViewController presentedViewController];

    if ([currentViewController isKindOfClass:[UINavigationController class]]) {

    currentViewController = [(UINavigationController *)currentViewController visibleViewController];

    NSLog(@"%@",[currentViewController class]);

    }

    }

    return currentViewController;

    }

    @end

    相关文章

      网友评论

        本文标题:根据自己项目获取到的当前控制器

        本文链接:https://www.haomeiwen.com/subject/rquovttx.html