美文网首页
iOS任何地方拿到当前控制器

iOS任何地方拿到当前控制器

作者: geekAppke | 来源:发表于2018-08-23 09:55 被阅读46次

    在子控件里,不是在控制器中(含导航),不能push\n
    NSObject加法声明、减法实现,方便调用!

    @interface NSObject (currentViewController)
    + (BNBaseViewController *)currentViewController;
    @end
    
    @implementation NSObject (currentViewController)
    - (BNBaseViewController *)currentViewController {
        BNBaseNavigationController *rootNavController = (BNBaseNavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    
        if ([rootNavController isKindOfClass:[BNBaseNavigationController class]]) {
            BNBaseViewController *currentVC = rootNavController.viewControllers.lastObject;
            return currentVC;
        } else {
            return nil;
        }
    }
    

    使用

    + (void)checkUserInfo {
        AccountSetViewController *vc = [[AccountSetViewController alloc] init];
        [[self currentViewController].navigationController pushViewController:vc animated:YES];
    }
    

    相关文章

      网友评论

          本文标题:iOS任何地方拿到当前控制器

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