How to get the active view in an iOS application f

2019-03-17 22:51发布

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

8条回答
Bombasti
2楼-- · 2019-03-17 23:31

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.

查看更多
劫难
3楼-- · 2019-03-17 23:32

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

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

查看更多
冷血范
4楼-- · 2019-03-17 23:33

Hope this helps you

UINavigationController *navCnt = (UINavigationController *)self.window.rootViewController;

if([navCnt.topViewController isMemberOfClass:[WebViewController class]])
{
    return UIInterfaceOrientationMaskAll;
}
else
return UIInterfaceOrientationMaskPortrait;
查看更多
看我几分像从前
5楼-- · 2019-03-17 23:33

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;
查看更多
老娘就宠你
6楼-- · 2019-03-17 23:33

Try Eric's answer:

+ (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}
查看更多
再贱就再见
7楼-- · 2019-03-17 23:36

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;
查看更多
登录 后发表回答