收到 NSNotification 后导航到特定的 UIViewController [英] Navigate to a specific UIViewController after receiving a NSNotification

查看:20
本文介绍了收到 NSNotification 后导航到特定的 UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 UINavigationController 处理 UIViewController 对象层次结构的 iOS 应用程序:

I am developing an iOS application that handles a hierarchy of UIViewController objects using a UINavigationController:

MenuViewController
  |
  |-ListOfAnimalsViewController
    |
    |-AnimalDetailsViewController
  |
  |-ListOfPlantsViewController
    |
    |-PlantDetailsViewController

应用程序接收带有某种动物或植物信息的本地NSNotification 对象.触摸通知时的通常行为是打开应用程序并加载层次结构中的第一个视图控制器.

The application receives local NSNotification objects with information of a certain animal or plant. The usual behavior when you touch the notification is to open the application and load the first view controller in hierarchy.

有没有办法以编程方式导航到层次结构深处的 UIViewController 实例?

Is there a way of programmatically navigating to an instance of a UIViewController deep in the hierarchy instead?

我不是要求将控制器推送到导航堆栈中,而是要求推送之前的控制器,即我想保留上面的导航架构.

I am not asking for pushing the controller into the navigation stack, but for pushing the previous controllers as well, i.e. I would like to keep the navigation schema above.

推荐答案

我将不得不不同意 Suresh.虽然此解决方案可能适用于这样的简单情况,即您只有 1 个以前的 ViewController,但如果您想添加第 10 个 ViewController 并保留整个层次结构,该怎么办?首先,您必须通过每个 ViewController 传递要显示的最后一段数据(在本例中为植物或动物),并且您将一次创建 10 个 ViewController.当前 ViewController 和第 10 个之间的转换远非无缝,性能会很糟糕.也不需要去创建自己的导航系统,不需要让事情变得更复杂,因为这个问题并不太难.

I'm gonna have to disagree with Suresh. While this solution may work in a simple case like this where you only have 1 previous ViewController, what if you wanted to add a 10th ViewController and keep the entire hierarchy? First of all you'd have to pass the final piece of data you want to show (in this case the plant or animal) through every ViewController and you'd be creating 10 ViewControllers at once. The transition between the current ViewController and the 10th would be far from seamless, performance would be terrible. Also no need to go and create your own navigation system, no need to make things more complicated than they are since this issue isn't too difficult.

只需推送您想要显示的 ViewController,这样您就只会创建 1 个 ViewController.在可以作为通知结果推送的每个 ViewController(以及其下的所有 ViewController)中,覆盖后退按钮的行为.使用 NSStringFromClass 检查导航控制器的 viewControllers 属性,如果之前的 ViewController 是您期望的那个.如果没有,请创建一个 NSMutableArray 作为 viewControllers 的副本,创建您期望的 ViewController 并将其插入数组中倒数第二个位置.然后通过调用带有可变数组动画 NO 的导航控制器上的 setViewControllers:animated: 方法替换整个堆栈.最后做流行音乐.同样,您将一次只创建 1 个 ViewController,从而保持最佳性能.

Just push the ViewController you want to show, that way you'll only be creating 1 ViewController. In every ViewController that can be pushed as a result of a notification (and all the ones below that), override the behavior of the back button. Check in the viewControllers property of the navigationController if the ViewController before is is the one you expect, using NSStringFromClass. If not, create an NSMutableArray as a copy of the viewControllers, create the ViewController you do expect and insert it at the second to last spot in the array. Then replace the entire stack by calling the setViewControllers:animated: method on the navigationController with the mutable array, animated NO. Finally do the pop. Again you'll have created just 1 ViewController at a time, keeping your performance optimal.

我现在无法发布代码,因为我使用的是 iPad,但如果您需要它,请询问,当我拥有真正的键盘时,我会添加一个示例.

Ican't post code right now since I'm on an iPad but if you need it, just ask and I'll add an example when i have a real keyboard.

这篇关于收到 NSNotification 后导航到特定的 UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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