title: get top visible view controller in objective c iOS
body: <p>I am implementing this method but it always returns me rootviewcontroller, not current visible controller how can I find out a visible controller. I also follow this link. <a href="https://stackoverflow.com/questions/6131205/iphone-how-to-find-topmost-view-controller">iPhone -- How to find topmost view controller</a>. but the same problem yet. </p>
  
  <pre><code>    + (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];
  }
  </code></pre>