iOS 获得屏幕正在显示的Controller(自定义的控制器)的实用方法
2016-11-16 15:15
316 查看
项目有点紧张,好久没有写点东西了。后面我会抽更多时间学习和分享学习的一些总结,分享给各位,请持续关注。
具体场景我都不说了。直接上代码
网上还有另外一种方法 (经测试只拿到当前 活活动窗口的根视图 不推荐)
顺便分享一个 字符串为空判断的宏定义 很高效
本期暂时到这里,需要什么新的ios相关知识,也可以给我留言哦,我们一起学习研究
具体场景我都不说了。直接上代码
- (void)needShowLogin { YPJGLoginViewController * login = [[YPJGLoginViewController alloc] init]; login.hidesBottomBarWhenPushed = YES; UIViewController * VC = [self currentViewController]; YPJGLog(@"当前显示的控制器%@",VC); //判断是否已经显示登录窗口,没有显示则push显示 if (![[NSString stringWithUTF8String:object_getClassName(VC)] isEqual:[NSString stringWithUTF8String:object_getClassName(login)]]) { [VC.navigationController pushViewController:login animated:YES]; } } //获取Window当前显示的ViewController - (UIViewController*)currentViewController{ //获得当前活动窗口的根视图 UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController; while (1) { //根据不同的页面切换方式,逐步取得最上层的viewController if ([vc isKindOfClass:[UITabBarController class]]) { vc = ((UITabBarController*)vc).selectedViewController; } if ([vc isKindOfClass:[UINavigationController class]]) { vc = ((UINavigationController*)vc).visibleViewController; } if (vc.presentedViewController) { vc = vc.presentedViewController; }else{ break; } } return vc; }
网上还有另外一种方法 (经测试只拿到当前 活活动窗口的根视图 不推荐)
- (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; }
顺便分享一个 字符串为空判断的宏定义 很高效
#define IsNOTNullOrEmptyOfNSString(string) !((![string isKindOfClass:[NSString class]])||[string isEqualToString:@""] || (string == nil) || [string isEqualToString:@"<null>"]|| [string isEqualToString:@"(null)"]|| [string isEqualToString:@"null"]|| [string isEqualToString:@"nil"] || [string isKindOfClass:[NSNull class]]||[[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
本期暂时到这里,需要什么新的ios相关知识,也可以给我留言哦,我们一起学习研究
相关文章推荐
- iOS 获得屏幕正在显示的Controller(自定义的控制器)的实用方法
- [iOSTabBarController自定义] 关联控制器和按钮的方法就是 selectedIndex = tag
- iOS 获得当前屏幕所显示的视图控制器
- iOS 获得当前页面正在显示的页面是哪一个Viewcontroller
- iOS开发UITabBarController的几种代理方法以及结合NSUserDefaults还原上次退出时被选中视图控制器和视图控制器的顺序
- iOS自定义导航控制器侧滑返回的多种实现方法
- iOS获取当前正在显示的窗口、视图控制器、视图
- iOS 获取当前正在显示的ViewController
- Android获得当前正在显示的activity类名的方法
- iOS 获取当前正在显示的ViewController或者当前的PresentedViewController
- iOS swift-自定义转场动画_实现modal控制器显示与消失时的透明度渐变
- iOS 判断 当前ViewController是否正在显示
- iOS获取当前屏幕控制器的方法
- IOS 之 通过 storyboard 自定义控制器以及 loadView 方法简述
- iOS 一个ViewController上显示2个tableView的方法
- 【iOS开发-30】UITabBarController的几种代理方法以及结合NSUserDefaults还原上次退出时被选中视图控制器和视图控制器的顺序
- 猫猫学iOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题
- iOS自定义导航控制器侧滑返回的多种实现方法
- 获取当前屏幕显示的视图控制器viewcontroller
- iOS自定义控制器侧滑半屏显示