简体   繁体   English

如何在iOS中找到当前可见的ViewController

[英]How to find current visible viewController in iOS

We know , if your viewController have been contain UINavigationController , 我们知道,如果您的viewController已包含UINavigationController,

you can find your current visible view controller by 'self.navigationController.visibleViewController' . 您可以通过'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 --->现在,显示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 --->现在,显示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 . --->错误,无法显示cvclr ,因为avclr是rootViewController,而avclr存在bvclr ,因此显示bvclr

Question: 题:

But we know ,code three in another .m file , so we don't know who is the rootViewController . 但是我们知道,在另一个.m文件中编码三个,所以我们不知道谁是rootViewController。 so If I present cvclr , the result is unexpect ! 因此,如果我显示cvclr ,结果将是意外的!

In the circumstances ,how to display cvclr 在这种情况下,如何显示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.. 如果要显示该类的下一个屏幕,则无需从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] ;

This code also check UITabbarViewContoller : 此代码还检查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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS中的当前ViewController之前找到所有View Controller? - How to find all the view controllers before my current viewcontroller in iOS? 如何从AppDelegate获取当前可见的viewController - How to get the current visible viewController from AppDelegate 如何在非viewcontroller类中获取可见/当前viewcontroller? 迅速 - How get visible / current viewcontroller in non-viewcontroller class? Swift iOS:如何在当前上下文中推送ViewController? - iOS: how to push ViewController over current context? 当用户在 iOS 中导航屏幕时,如何跟踪当前可见 ViewController 的变化? - How to track changes in currently visible ViewController as user navigating the screens in iOS? iOS:如何获取当前可见的键盘类型? - iOS: How to get the current visible keyboard type? 如何重写ScrollViewDidEndDecelerating并找到当前可见的单元格? - How to Override ScrollViewDidEndDecelerating and find current visible cell ? iOS在当前ViewController中设置BackButton - iOS set BackButton in the current ViewController 如何通过IOS Swift中的任何后台进程获取当前的viewcontroller - How to get current viewcontroller through any background process in IOS Swift 如何找到属于当前viewController的导航控制器? - How do I find out the navigation controller that is part of the current viewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM