Skip to content

Instantly share code, notes, and snippets.

@Quarezz
Forked from snikch/gist:3661188
Created April 12, 2016 10:31
Show Gist options
  • Select an option

  • Save Quarezz/3515bffb498160166351527cde2e99a0 to your computer and use it in GitHub Desktop.

Select an option

Save Quarezz/3515bffb498160166351527cde2e99a0 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];
    }