简体   繁体   English

如何确定哪个视图控制器当前处于活动状态/显示视图的视图控制器?

[英]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.

相关问题 如何检查并确定情节提要中当前显示的视图控制器? - How can I check and determine which view controller in storyboard is currently showing? 如何确定哪个视图处于活动状态? - How can I determine which view is active? 如何确定视图控制器 - how to determine view controller 如何确定哪个tabbar索引是我的视图控制器 - how to determine which tabbar index is my view controller iOS如何知道哪个视图控制器应该处于活动状态? - How does iOS know which view controller should be active? iOS:显示UIAlert是否会破坏当前显示的模式视图控制器? - iOS: Can displaying a UIAlert disrupt a currently displayed modal view controller? 使用根视图控制器确定要加载的视图 - Using a root view controller to determine which view to load 如何在一个视图控制器中将数据保存到 CoreData 中,然后在另一个视图控制器中让 tableview 显示数据? - How to save data into CoreData in one view controller, and then have a tableview displaying the data in another view controller? 如何将数据从一个视图控制器传递到导航控制器中嵌入的另一个视图控制器 - How to pass data from one view controller to another view controller which is embedded in navigation controller 如何在TabbarController中确定当前的活动视图 - How to determine the current active view in tabbarcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM