24

how can i get the currently active view (the main view currently being viewed by the user) from the app delegate for references sake?

Maxim Mikheev
  • 1,984
  • 3
  • 16
  • 33
David
  • 241
  • 1
  • 2
  • 3
  • Define "main view". There could be several `UIView` objects (and subclasses) on-screen at once. Which one would you expect to get? – Shaggy Frog Sep 14 '10 at 00:54
  • Say the user is currently on a screen to perform a specific action, I want to get the viewcontroller for the container view for that screen – David Sep 14 '10 at 01:39
  • You'll have to keep track of that yourself. Like views, there could be more than one view controller active on the screen. – Shaggy Frog Sep 14 '10 at 03:08

8 Answers8

34
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;
phatmann
  • 17,007
  • 6
  • 57
  • 47
16

All top answers doesn't work with modal presented views! Use this code instead:

UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];

skywinder
  • 20,546
  • 15
  • 87
  • 122
9

It depends on how your app is set up.

Typically, your app delegate will have a main view controller property that will let you get to it.

To get your app delegate

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

UIView *topView = appDelegate.viewController.view;

If your app has a navigation controller property in the app delegate you can do something like this.

UIView *topView = appDelegate.navigationController.topViewController.view;
Randall
  • 14,070
  • 7
  • 36
  • 58
  • The third line gives me an error: Property 'navigationController' not found on object of type 'kzAppDelegate' – zakdances May 11 '12 at 15:44
  • you probably aren't using a navigation controller then. Look at the second line of code in my answer. – Randall May 11 '12 at 23:59
1

I found this and it works great for me for all types of segues and view controllers.

+(UIViewController*) getTopController{
    UIViewController *topViewController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topViewController.presentedViewController) {
        topViewController = topViewController.presentedViewController;
    }

    return topViewController;
}
Eyal Cinamon
  • 939
  • 5
  • 15
mdimarca
  • 163
  • 11
0

you can use [appDelegate.navigationController.topViewController class] to log the class of the controller or get the title property to know which one it is.

Farrukh Javeid
  • 634
  • 6
  • 24
0

Hope this helps you

UINavigationController *navCnt = (UINavigationController *)self.window.rootViewController;

if([navCnt.topViewController isMemberOfClass:[WebViewController class]])
{
    return UIInterfaceOrientationMaskAll;
}
else
return UIInterfaceOrientationMaskPortrait;
Mehmood
  • 905
  • 5
  • 15
0

Anywhere in the app you can get your rootViewController View also like that:

id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
UIView *rootView = appDelegate.window.rootViewController.view;
Krzysztof Przygoda
  • 1,045
  • 1
  • 12
  • 23
0

Try Eric's answer:

+ (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}
Community
  • 1
  • 1
Hsm
  • 1,430
  • 15
  • 16