[英]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 ? 但是我给您介绍一个视图控制器,如何找到当前的可见控制器?
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
。
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.