如何从应用程序委托获取 iOS 应用程序中的活动视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3705410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the active view in an iOS application from the app delegate?
提问by David
how can i get the currently active view (the main view currently being viewed by the user) from the app delegate for references sake?
我如何从应用程序委托中获取当前活动视图(用户当前正在查看的主视图)以供参考?
回答by phatmann
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;
回答by skywinder
All top answers doesn't work with modal presented views!Use this code instead:
所有最佳答案都不适用于模态呈现的视图!请改用此代码:
UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
回答by Randall
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;
回答by mdimarca
I found this and it works great for me for all types of segues and view controllers.
我发现了这个,它适用于所有类型的 segue 和视图控制器。
+(UIViewController*) getTopController{
UIViewController *topViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topViewController.presentedViewController) {
topViewController = topViewController.presentedViewController;
}
return topViewController;
}
回答by Hsm
Try Eric's answer:
试试埃里克的回答:
+ (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
回答by Krzysztof Przygoda
Anywhere in the app you can get your rootViewController View also like that:
在应用程序的任何地方,您都可以像这样获得 rootViewController 视图:
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
UIView *rootView = appDelegate.window.rootViewController.view;
回答by Farrukh Javeid
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.
您可以使用[appDelegate.navigationController.topViewController class]
来记录控制器的类或获取 title 属性以了解它是哪一个。
回答by Mehmood
Hope this helps you
希望这对你有帮助
UINavigationController *navCnt = (UINavigationController *)self.window.rootViewController;
if([navCnt.topViewController isMemberOfClass:[WebViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
else
return UIInterfaceOrientationMaskPortrait;