如何确定ViewController是否为顶级?

编程入门 行业动态 更新时间:2024-10-20 03:35:37
本文介绍了如何确定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是否为顶级?

本文发布于:2023-11-29 17:14:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ViewController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!