iOS任何地方拿到当前控制器
2018-08-23 本文已影响46人
geekAppke
在子控件里,不是在控制器中(含导航),不能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];
}