如何在iOS

时间:2016-10-25 08:24:19

标签: ios presentviewcontroller

我们知道,如果你的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

4 个答案:

答案 0 :(得分:1)

找到我使用此方法的当前顶视图控制器

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

如果您要展示该课程的下一个屏幕,则无需从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] ;

答案 3 :(得分:0)

此代码还会检查$(function() { if ( window.location.search.indexOf('hideSlctRgn=1') != -1 ) { $("#slctRgn").hide(); $("#lgnPage").show(); } })

UITabbarViewContoller