how can i get the currently active view (the main view currently being viewed by the user) from the app delegate for references sake?
-
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 Answers
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;

- 18,161
- 7
- 61
- 51
All top answers doesn't work with modal presented views! Use this code instead:
UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];

- 21,291
- 15
- 93
- 123
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;

- 14,691
- 7
- 40
- 60
-
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
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;
}

- 939
- 5
- 16

- 163
- 11
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.

- 634
- 6
- 25
Hope this helps you
UINavigationController *navCnt = (UINavigationController *)self.window.rootViewController;
if([navCnt.topViewController isMemberOfClass:[WebViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
else
return UIInterfaceOrientationMaskPortrait;

- 933
- 5
- 17
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;

- 1,217
- 1
- 17
- 24
Try Eric's answer:
+ (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}