使用另一个UINavigationController推送视图控制器 [英] Pushing a View Controller using another UINavigationController

查看:89
本文介绍了使用另一个UINavigationController推送视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的应用程序的样子。我以一种方式子类化UINavigationController,当你点击类似于导航栏的一个按钮MenuViewController滑出。我如何推送\呈现一个我的VC1,VC2,VC3进入ContentViewController从didSelectRow在UITableViewController菜单?

  - > SlidingViewController 
/ \
容器容器
| |
MenuViewController ContentViewController
| |
UITableViewController SubClassed UINavigationController
| | | |
VC1 VC2 VC3 VC4

通常我做类似下面的代码,但是上面的情况我不知道如何可以推送一个新的视图控制器,如果我不在同一个UINavigationController。

  NSString * storyboardName = @MainStoryboard_iPhone; 
NSString * viewControllerID = @ViewID;
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller =(MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[? presentViewController:controller animated:YES completion:nil];



编辑



可能的解决方案,但我不知道,如果这是一个很好的方法来做。



我的ContentViewController内部:

  [DataManager sharedDataManager] .navController = self.navigationController; 

在MenuViewController中的didSelectRow内:

  [[DataManager sharedDataManager] .navController presentViewController:controller animated:YES completion:nil]; 


解决方案

MenuViewController 以了解视图控制器层次结构中除本身及其子项之外的任何内容。您有两个选项,允许这个好的设计。



1。使用委托



首先,您需要一个 MenuViewControllerDelegate ,例如 menuViewController:didSelectOption:。当菜单中发生某些事情时, MenuViewController 将向其委托对象发送适当的委托消息。



对于该委托消息,您将需要一个符合 MenuViewControllerDelegate 的控制器对象(可以是视图控制器或只是一个常规的 NSObject $ c>并将 MenuViewController 的委托设置到自身。然后,这个控制器对象将处理 MenuViewController 可能发送的任何委托消息。当选择菜单选项时,将新的视图控制器推送到 ContentViewController



2。发布通知



这两个更脆弱。



因此,如果您的视图控制器层次结构中有很多东西需要知道菜单中发生什么事情,那么您将使用通知。 会发布通知,可能命名为 MenuViewControllerDidSelectOptionNotification ,然后已注册该通知的任何感兴趣的接收者将接收通知并执行他们的事情。


This is how my app looks like. I've subclassed UINavigationController in a way that when you tap a button on something similar to a navigation bar the MenuViewController slides out. How can I push \ present one of the my VC1, VC2, VC3 into ContentViewController from didSelectRow that's inside the UITableViewController menu?

                -> SlidingViewController
                 /                   \
            Container             Container       
                |                     |
        MenuViewController    ContentViewController
                |                      |
     UITableViewController   SubClassed UINavigationController
                                  |    |    |    |
                                 VC1  VC2  VC3  VC4

Usually I do something like the next code but with the situation above I'm not sure how I can push a new view controller if i'm not in the same UINavigationController.

NSString * storyboardName = @"MainStoryboard_iPhone";
NSString * viewControllerID = @"ViewID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[? presentViewController:controller animated:YES completion:nil];

Edit:

Possible solution that works but I'm not sure if that's a good way to do it. I've used a singleton to save my Navigation Controller and use it later.

Inside my ContentViewController:

[DataManager sharedDataManager].navController = self.navigationController;

Inside my didSelectRow in MenuViewController :

[[DataManager sharedDataManager].navController presentViewController:controller animated:YES completion:nil];

解决方案

You don't want MenuViewController to have knowledge of anything in the view controller hierarchy other than itself and its children. You have two options that allow for this good design.

1. Use delegation

First, you want to have a MenuViewControllerDelegate that has, for example, menuViewController:didSelectOption:. When something happens in the menu, MenuViewController will send an appropriate delegate message to its delegate object.

Now to listen for that delegate message, you will need a controller object (could be a view controller or just a regular NSObject) that conforms to MenuViewControllerDelegate and sets the delegate of MenuViewController to itself. This controller object will then handle any delegate messages that MenuViewController may send, e.g. push a new view controller onto ContentViewController when a menu option is selected.

2. Post notifications

This is the more fragile of the two. You would use notifications if you have lots of things all over your view controller hierarchy that need to know when something happens in the menu.

So MenuViewController would post a notification, perhaps named MenuViewControllerDidSelectOptionNotification, and then any interested receivers that have registered for that notification would then receive the notification and do their thing.

这篇关于使用另一个UINavigationController推送视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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