Steve Gear
Steve Gear

Reputation: 749

Get top most view controller under UIAlertController

I am using below code to find the top most ViewController. If alert is presented, the code above gives UIAlertController. How do I get top view controller under UIAlertController?

+(UIViewController*)topMostViewController:(UIViewController*)rootViewController
{

    if ([rootViewController isKindOfClass:[UITabBarController class]])
    {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topMostViewController:tabBarController.selectedViewController];
    }
    else if ([rootViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topMostViewController:navigationController.visibleViewController];
    }
    else
    {
        return rootViewController;
    }
}

Any idea?

Upvotes: 1

Views: 348

Answers (1)

subin272
subin272

Reputation: 753

Updated the answer. Since you want to find the top most view controller that is visible, you can you any of these two methods. Call this method from the view controller you are currently in by passing the UINavigation controller as parameter.

func  topMostViewController(controller:UINavigationController) -> UIViewController {

    return controller.topViewController!

}

func  visibleViewController(controller:UINavigationController) -> UIViewController {

    return controller.visibleViewController!

}

Upvotes: 0

Related Questions