[英]How to determine which view controller is currently active/the one displaying a view?
In my app I am queueing some local notifications, when they fire I must present a modal view. 在我的应用程序中,我正在排队一些本地通知,当他们开火我必须提出模态视图。 The trouble is I have numerous view controllers any one of which could currently be active and thus the one that needs to present the modal view controller.
问题是我有许多视图控制器,其中任何一个当前都可以是活动的,因此需要提供模态视图控制器。 How can I determine which one is currently in use?
如何确定当前使用的是哪一个?
I am setting a navigation controller as the windows root view controller, and this can push any number of other view controllers, some of them themselves may also be currently presenting another view controller modally. 我正在将导航控制器设置为Windows根视图控制器,这可以推送任意数量的其他视图控制器,其中一些本身也可能正在以模态方式呈现另一个视图控制器。 This must work on iOS 4 and 5.
这必须适用于iOS 4和5。
I have a lot of view controllers so would like to avoid putting code in each of them to each check if they are currently the top one. 我有很多视图控制器,所以我们希望避免将每个视图中的代码放到每个检查中,如果它们当前是最顶层的。
You can look at the navigation controller's topViewController
property to find out which controller is at the top of the stack. 您可以查看导航控制器的
topViewController
属性,找出哪个控制器位于堆栈顶部。 This will be the one whose view is displayed. 这将是显示其视图的那个。
Since you may also be presenting a modal view controller, you'll probably be more interested in the visibleViewController
property, which will give you the controller for the current view whether its presented modally or pushed onto the navigation stack. 由于您可能还在展示模态视图控制器,因此您可能对
visibleViewController
属性更感兴趣,该属性将为您提供当前视图的控制器,无论其是以模态方式呈现还是推送到导航堆栈。
Create a variable that stores a pointer to the ViewController that was most recently pushed. 创建一个变量,该变量存储指向最近推送的ViewController的指针。 Every time you push a new ViewController, update this variable.
每次推送新的ViewController时,都要更新此变量。 Then you'll always know which one is on the top!
然后你总会知道哪一个在顶部!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.