How to Find Current Visible Viewcontroller in Ios

How to find current visible viewController in iOS

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

How to get the current visible viewController from AppDelegate

Do conditional casting on the return value to safely check its type.

if let currentVC = UIApplication.topViewController() as? MyViewController {
//the type of currentVC is MyViewController inside the if statement, use it as you want to
}

Your whole function implementation is flawed, if it actually worked, it would lead to infinite recursion. Once you find out the type of your current top view controller in your if statements, you are calling the same function again with the current root controller as its input value. Your function only ever exists, if it reaches either a call from a view controller, whose class is none of the ones specified in your optional bindings.

Moreover, your whole implementation doesn't do anything at the moment. You find out the type of your root view controller, but than you upcast it by returning a value of type UIViewController.

How to get visible viewController from app delegate when using storyboard?

This should do it for you:

- (void)applicationWillResignActive:(UIApplication *)application
{
UIViewController *vc = [self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController *)visibleViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil)
{
return rootViewController;
}
if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];

return [self visibleViewController:lastViewController];
}
if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]])
{
UITabBarController *tabBarController = (UITabBarController *)rootViewController.presentedViewController;
UIViewController *selectedViewController = tabBarController.selectedViewController;

return [self visibleViewController:selectedViewController];
}

UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;

return [self visibleViewController:presentedViewController];
}

Get the current displaying UIViewController on the screen in AppDelegate.m

You can use the rootViewController also when your controller is not a UINavigationController:

UIViewController *vc = self.window.rootViewController;

Once you know the root view controller, then it depends on how you have built your UI, but you can possibly find out a way to navigate through the controllers hierarchy.

If you give some more details about the way you defined your app, then I might give some more hint.

EDIT:

If you want the topmost view (not view controller), you could check

[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];

although this view might be invisible or even covered by some of its subviews...

again, it depends on your UI, but this might help...

Current visible view controller checking

The problem is that you instantiate a new object of ParentEndViewController when you call the [storyboard instantiateViewControllerWithIdentifier:@"ParentEndViewController"]; this instance is not the same as the instance of your root view controller.
If you are checking the root view controller of your app in app delegate you should try

if([self.window.rootViewController isKindOfClass:[ParentEndViewController class]]) {
NSLog(@"Luke I'm your father");
}
else {
NSLog(@"Sorry bro, somebody else is the parent");
}

If you are checking the last view controller of your navigation controller you should try something like:

UIViewController *lastViewController = [[self.navigationController viewControllers] lastObject];

if([lastViewController isKindOfClass:[ParentEndViewController class]) {
NSLog(@"Luke I'm your father");
}
else {
NSLog(@"Sorry bro, somebody else is the parent");
}

How to find topmost view controller on iOS

iOS 4 introduced the rootViewController property on UIWindow:

[UIApplication sharedApplication].keyWindow.rootViewController;

You'll need to set it yourself after you create the view controller though.

get top visible view controller in objective c iOS

Finally i resolve this issue by adding more code. like this...

UINavigationController *vc = SharedAppDelegate.navigationController;
NSLog(@"%@",vc.viewControllers);
UIViewController* controller = vc.viewControllers.firstObject;
NSLog(@"%@",controller.childViewControllers);
UIViewController* carbonContr = controller.childViewControllers.firstObject;


Related Topics



Leave a reply



Submit