how can i get the currently active view (the main view currently being viewed by the user) from the app delegate for references sake?
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.
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.
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).
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;
All top answers doesn't work with modal presented views! Use this code instead:
UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With