开发者

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?


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜