Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the active view in an iOS application from the app delegate?

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

like image 528
David Avatar asked Sep 14 '10 00:09

David


People also ask

How do I get current ViewController?

You can now call visibleViewController on UIWindow and this will get you the visible view controller by searching down the controller hierarchy. This works if you are using navigation and/or tab bar controller. If you have another type of controller to suggest please let me know and I can add it.

What is IOS app delegate?

The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.

How do I get Windows on Swiftui?

In your ContentView create a button and open a URL for your app and another View e.g. Viewer to be shown in the window we will open[…] In your App add another WindowGroup for your viewer and set it to enable handling of external launch events (an internal event in our case).


3 Answers

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;
like image 60
phatmann Avatar answered Oct 20 '22 17:10

phatmann


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

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

like image 26
skywinder Avatar answered Oct 20 '22 17:10

skywinder


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;
like image 9
Randall Avatar answered Oct 20 '22 16:10

Randall