来自 AppDelegate 的当前视图控制器?
有没有办法从 AppDelegate 获取当前视图控制器?我知道有 rootViewController,但这不是我要找的。
Is there a way to get the current view controller from the AppDelegate? I know there is rootViewController, but that's not what I'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您的应用程序的根视图控制器是
UINavigationController
,您可以这样做:同样,如果它是
UITabBarController
,您可以这样做:当然,像这样的显式转换是肮脏的。更好的方法是使用强类型自己捕获引用。
If your app's root view controller is a
UINavigationController
you can do this:Similarly, if it's a
UITabBarController
you can do this:Of course, explicit casting like this is dirty. Better would be to capture the reference yourself using strong types.
这可能对
Swift 版本有帮助:
取自:
https://gist.github.com/snikch/3661188
This might help
Swift version:
Taken from:
https://gist.github.com/snikch/3661188
如果您将 UINavigationController 放入 appDelegate 中,则使用其属性 topViewController 或 visibleViewController
If you have UINavigationController into appDelegate then use its property topViewController or visibleViewController
进行扩展:
用法:
Make an extension:
Usage:
获取 appDelegate 对象:
正如 beryllium 建议的那样,您可以使用 UINavigationController 的属性来访问当前的视图控制器。
所以代码看起来像:
或:
Get the appDelegate object:
As beryllium suggested you can use the UINavigationController's properties to access your current view controller.
So the code would look like:
or:
您可以通过查找它的presentedViewController从rootViewController获取当前视图控制器,如下所示:
它与我一起工作。希望它有帮助:)
You can get the current view controller from rootViewController by looking for its presentedViewController, like this:
It works with me. Hope it helps :)
对于任何不使用
UINavigationController
但其默认视图控制器是UIViewController
的人,您可以使用以下命令检查哪个视图控制器处于活动状态(或呈现) 正如您所看到的,我正在检查当前视图控制器,以便支持特定视图控制器的不同界面方向。对于任何有兴趣使用此方法来支持特定方向的人,应将以下内容放置在需要特定方向的每个视图控制器中。
注意:此代码是使用 Swift 1.2 编写的
For anyone not using a
UINavigationController
but rather their default view controller is aUIViewController
you can check which view controller is active (or presented) with the following inAppDelegate
:As you can see I'm checking for the current view controller in order to support different interface orientations for specific view controllers. For anyone else interested in using this method to support specific the following should be placed in each view controller that needs a specific orientation.
Note: This code was written with Swift 1.2
UIApplication 扩展采用 Swift 4+ 语法,基于 AG 的解决方案
示例用法:
UIApplication extension in Swift 4+ syntax based on A.G's solution
Sample usage:
这应该可以满足您的需求。
That should get you what you need.
我经常需要检索当前显示的视图控制器。它可能意味着当前 UINavigationController 堆栈顶部的视图控制器、当前呈现的视图控制器等。所以我编写了这个函数,它在大多数情况下都能解决这个问题,并且您可以在 UIViewController 扩展中使用它。
Swift 3 中的代码:
使用以下方式调用:
currentViewController()
Often I need to retrieve the view controller that is currently displayed. It could mean the view controller at the top of the stack of the current UINavigationController, the currently presented view controller, etc. So I wrote this function which figures it out most of the time, and that you can use inside a UIViewController extension.
Code in Swift 3:
Call it with:
currentViewController()
如果有人想要 Objective C。
GlobalManager.h
GlobalManager.m
如何使用。
If anyone wants in Objective C.
GlobalManager.h
GlobalManager.m
How to use.