如何确定ViewController是否为顶级? [英] How to determine if a ViewController is top level or not?

查看:70
本文介绍了如何确定ViewController是否为顶级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签栏驱动的应用程序.

I have a tab bar driven app.

该应用程序具有一些视图控制器,它们以通常的方式相互堆叠.这些视图控制器中的某些可能是顶级的-意味着直接从选项卡栏控制器实例化.它们的导航栏中没有后退"按钮.

The app has some view controllers that are stacked on each other the usual way. Some of these view controllers may be top level - meaning instantiated directly from the tab bar controller. Those do not have a "back" button in their navigation bar.

其中一些可以在调用堆栈中的许多点调用.意味着可以从其他视图控制器中调用它们,而不能直接从选项卡栏中调用它们.

Some of these can be called at many points within the call stack. Meaning they could be called from some other view controller and not directly from the tab bar.

我正在寻找一种聪明的方法来以编程方式确定当前视图控制器当前在其导航栏中是否具有后退"按钮.(如果不是,那么我想在那个地方显示其他栏项目.)

I am looking for a smart way to determine programatically whether the current view controller currently has a "back" button in its nav bar or not. (If it does not then I want to display some other bar item at that place.)

任何提示都值得赞赏.:)

Any hint is appreciated. :)

推荐答案

从您描述的内容中,我可以理解,您有一个 UITabBarController ,每个视图中都有一个视图控制器列表标签,每个视图控制器都是一个导航控制器,这会进一步带来新的视图.并且您有兴趣查找某个特定的视图是第一个视图还是第二个视图,依此类推.

Well... from what you are describing, I can understand that you have a UITabBarController with a list of view controller in each tab, and each view controller is a navigation controller, which further leads to new views. And you are interested in finding if a particular view is the first view or the second view, and so forth.

有趣的是,有一种方法可以做到这一点.导航控制器基本上是一个 viewController 数组,其中的视图已添加到navigationController的索引中.第一个视图将在索引0中.如果执行 pushViewController ,则 newViewController 将放置在索引1处.

Interestingly, there is a way to do this. The navigation controller is basically a viewController array with views added to the index of the navigationController. The first view will be in index 0. If you perform a pushViewController, the newViewController is placed at index 1.

只需检查该特定视图的 self.navigationController 的索引计数.如果大于1,则可以确定它不是初始视图.

Just check for the index count of the self.navigationController of that particular view. If it is greater than 1, you can be certain that it is not the initial view.

您还可以检查 backBarButtonItem 属性,但是同样,可能不需要. viewControllers 数组计数应该可以.

You can also check for the backBarButtonItem property, but then again, it may not be needed. The viewControllers array count should do.

这篇关于如何确定ViewController是否为顶级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆