简体   繁体   中英

How to find current visible viewController in iOS

We know , if your viewController have been contain UINavigationController ,

you can find your current visible view controller by 'self.navigationController.visibleViewController' .

But I you present a view controller , how to find current visible controller ?

For Example :

code one :
------
AVClr *avclr = [[AVClr alloc]init] ;
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate ;
appDelegate.window.rootViewController = avclr ;
[avclr presentViewController:loginNavClr animated:YES completion:nil] ;

---> now , display 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] ;

---> now , display 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] ;

---> Error , can not display cvclr , because avclr is a rootViewController and avclr present bvclr , so display bvclr .

Question:

But we know ,code three in another .m file , so we don't know who is the rootViewController . so If I present cvclr , the result is unexpect !

In the circumstances ,how to display cvclr

to find current top view controller i used this method

- (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;
}
-(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;
}

If you are presenting the next screen from that class then you don't need to fetch top view controller from UIWindow Simply use this..

 -----------------
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] ;

This code also check 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM