如何获得对当前UINavigationController的引用? [英] How can I get a reference to the current UINavigationController?
问题描述
在Objective-C中,获取对当前UINavigationController的引用的最佳方法是什么?我想从可能没有引用UIController,委托或其他任何内容的任何类中获取对它的访问.
In Objective-C, what is the best way of getting a reference to the current UINavigationController? I would like to gain access to it from any class that may not have a reference to a UIController, delegate, or anything else.
是否存在获取当前UINavigationController的现有方法?我应该添加一些东西到我的应用程序委托中,然后检索它吗?
Is there an existing way to get the current UINavigationController? Should I add something to my application delegate, and retrieve that?
推荐答案
事实证明,尽管有很多点语法,您仍可以从任何UIView/UIViewController后代中获得它.
As it turns out, you can get it, albeit with quite a bit of dot syntax, from any UIView/UIViewController descendant.
self.(view).window.rootViewController.navigationController;
如果您使用的对象是任何其他类的后代,则同样如此,只是必须通过 UIApplication
If you are using an object that descends from any other class, the same is true, except you must go through UIApplication
[UIApplication sharedApplication].keyWindow.rootViewController.navigationController;
当然,这两者都在很大程度上破坏了MVC,因此绝对建议将必须接触默认导航控制器"的任何逻辑都添加到根视图控制器中.
Of course, both of these break MVC in a big way, and it is definitely recommended that any logic that must touch the "default navigation controller" be added to the root view controller.
这篇关于如何获得对当前UINavigationController的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!