如何在 iOS 中找到当前可见的 viewController
How to find current visible viewController in iOS
我们知道,如果你的 viewController 已经包含 UINavigationController ,
您可以通过 'self.navigationController.visibleViewController' 找到您当前的可见视图控制器。
但是我给你一个视图控制器,如何找到当前可见的控制器?
例如:
code one :
------
AVClr *avclr = [[AVClr alloc]init] ;
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate ;
appDelegate.window.rootViewController = avclr ;
[avclr presentViewController:loginNavClr animated:YES completion:nil] ;
---> 现在,显示 avclr
code two:
------
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate ;
UIViewController *currentVisibleViewController = appDelegate.window.rootViewController ;
BVClr *bvclr = [[BVClr alloc]init] ;
[currentVisibleViewController presentViewController:bvclr animated:YES completion:nil] ;
---> 现在,显示 bvclr
code three:
------
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate ;
UIViewController *currentVisibleViewController = appDelegate.window.rootViewController ;
CVClr *cvclr = [[CVClr alloc]init] ;
[currentVisibleViewController presentViewController:cvclr animated:YES completion:nil] ;
---> 错误,无法显示 cvclr
,因为 avclr
是一个 rootViewController 而 avclr
存在 bvclr
,所以显示 bvclr
。
问题:
但是我们知道,在另一个.m文件中编码了三个,所以我们不知道rootViewController是谁。所以如果我出现 cvclr
,结果是出乎意料的 !
情况下,如何显示cvclr
为了找到当前的顶视图控制器,我使用了这个方法
- (UIViewController *)currentTopViewController
{
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController)
{
topVC = topVC.presentedViewController;
}
if ([topVC isKindOfClass:[UINavigationController class]]) {
return [(UINavigationController *)topVC topViewController];
}
return topVC;
}
如果您要显示那个 class 的下一个屏幕,那么您不需要从 UIWindow
获取顶视图控制器
只需使用这个..
-----------------
AVClr *avclr = [[AVClr alloc]init];
[self presentViewController: avclr animated:YES completion:nil] ;
------------------------------
BVClr *bvclr = [[BVClr alloc]init] ;
[self.presentingViewControler presentViewController:bvclr animated:YES completion:nil] ;
------------------
CVClr *cvclr = [[CVClr alloc]init] ;
[self.presentingViewControler presentViewController:cvclr animated:YES completion:nil] ;
-(UIViewController *)getVisibleViewController : (UIViewController *)rootViewController
{
UIViewController *rootVC = rootViewController;
if (rootVC == nil)
{
rootVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
}
if ([rootVC presentedViewController] == nil)
{
return rootVC;
}
if ([rootVC presentedViewController] != nil)
{
if ([[rootVC presentedViewController] isKindOfClass:UINavigationController.self]) {
UINavigationController *navigationController = (UINavigationController *)[rootVC presentedViewController];
return [[navigationController viewControllers] lastObject];
}
return [self getVisibleViewController : [rootVC presentedViewController]];
}
return nil;
}
此代码还检查 UITabbarViewContoller
:
-(UIViewController *) getVisibleViewContoller {
UIViewController *rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController;
if (!rootViewController) {
return nil;
}
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabbarVC = (UITabBarController *) rootViewController;
UIViewController *selectedVC = tabbarVC.selectedViewController;
if (selectedVC) {
if (![selectedVC isKindOfClass:[UINavigationController class]]) {
return selectedVC;
}
rootViewController = selectedVC;
}
}
if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationVC = (UINavigationController *) rootViewController;
if (navigationVC.topViewController) {
return navigationVC.topViewController;
}
return navigationVC.viewControllers.lastObject;
}
return rootViewController;
}
我们知道,如果你的 viewController 已经包含 UINavigationController ,
您可以通过 'self.navigationController.visibleViewController' 找到您当前的可见视图控制器。
但是我给你一个视图控制器,如何找到当前可见的控制器?
例如:
code one :
------
AVClr *avclr = [[AVClr alloc]init] ;
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate ;
appDelegate.window.rootViewController = avclr ;
[avclr presentViewController:loginNavClr animated:YES completion:nil] ;
---> 现在,显示 avclr
code two:
------
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate ;
UIViewController *currentVisibleViewController = appDelegate.window.rootViewController ;
BVClr *bvclr = [[BVClr alloc]init] ;
[currentVisibleViewController presentViewController:bvclr animated:YES completion:nil] ;
---> 现在,显示 bvclr
code three:
------
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate ;
UIViewController *currentVisibleViewController = appDelegate.window.rootViewController ;
CVClr *cvclr = [[CVClr alloc]init] ;
[currentVisibleViewController presentViewController:cvclr animated:YES completion:nil] ;
---> 错误,无法显示 cvclr
,因为 avclr
是一个 rootViewController 而 avclr
存在 bvclr
,所以显示 bvclr
。
问题:
但是我们知道,在另一个.m文件中编码了三个,所以我们不知道rootViewController是谁。所以如果我出现 cvclr
,结果是出乎意料的 !
情况下,如何显示cvclr
为了找到当前的顶视图控制器,我使用了这个方法
- (UIViewController *)currentTopViewController
{
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController)
{
topVC = topVC.presentedViewController;
}
if ([topVC isKindOfClass:[UINavigationController class]]) {
return [(UINavigationController *)topVC topViewController];
}
return topVC;
}
如果您要显示那个 class 的下一个屏幕,那么您不需要从 UIWindow
获取顶视图控制器
只需使用这个..
-----------------
AVClr *avclr = [[AVClr alloc]init];
[self presentViewController: avclr animated:YES completion:nil] ;
------------------------------
BVClr *bvclr = [[BVClr alloc]init] ;
[self.presentingViewControler presentViewController:bvclr animated:YES completion:nil] ;
------------------
CVClr *cvclr = [[CVClr alloc]init] ;
[self.presentingViewControler presentViewController:cvclr animated:YES completion:nil] ;
-(UIViewController *)getVisibleViewController : (UIViewController *)rootViewController
{
UIViewController *rootVC = rootViewController;
if (rootVC == nil)
{
rootVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
}
if ([rootVC presentedViewController] == nil)
{
return rootVC;
}
if ([rootVC presentedViewController] != nil)
{
if ([[rootVC presentedViewController] isKindOfClass:UINavigationController.self]) {
UINavigationController *navigationController = (UINavigationController *)[rootVC presentedViewController];
return [[navigationController viewControllers] lastObject];
}
return [self getVisibleViewController : [rootVC presentedViewController]];
}
return nil;
}
此代码还检查 UITabbarViewContoller
:
-(UIViewController *) getVisibleViewContoller {
UIViewController *rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController;
if (!rootViewController) {
return nil;
}
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabbarVC = (UITabBarController *) rootViewController;
UIViewController *selectedVC = tabbarVC.selectedViewController;
if (selectedVC) {
if (![selectedVC isKindOfClass:[UINavigationController class]]) {
return selectedVC;
}
rootViewController = selectedVC;
}
}
if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationVC = (UINavigationController *) rootViewController;
if (navigationVC.topViewController) {
return navigationVC.topViewController;
}
return navigationVC.viewControllers.lastObject;
}
return rootViewController;
}