Skip to content

Instantly share code, notes, and snippets.

@rcholic
Forked from snikch/gist:3661188
Created July 24, 2022 06:27
Show Gist options
  • Save rcholic/8aafaa9af7d8dfc28ef76bcc8ce9d43b to your computer and use it in GitHub Desktop.
Save rcholic/8aafaa9af7d8dfc28ef76bcc8ce9d43b to your computer and use it in GitHub Desktop.

Revisions

  1. Mal Curtis created this gist Sep 6, 2012.
    19 changes: 19 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    - (UIViewController *)topViewController{
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    }

    - (UIViewController *)topViewController:(UIViewController *)rootViewController
    {
    if (rootViewController.presentedViewController == nil) {
    return rootViewController;
    }

    if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
    UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
    UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
    return [self topViewController:lastViewController];
    }

    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
    return [self topViewController:presentedViewController];
    }