how can i get the currently active view (the main view currently 开发者_开发百科being viewed by the user) from the app delegate for references sake?
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;
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;
}
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.
Hope this helps you
UINavigationController *navCnt = (UINavigationController *)self.window.rootViewController;
if([navCnt.topViewController isMemberOfClass:[WebViewController class]])
{
    return UIInterfaceOrientationMaskAll;
}
else
return UIInterfaceOrientationMaskPortrait;
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;
Try Eric's answer:
+ (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController;
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论