在应用程序中处理不同的视图控制器:创建自定义内容视图控制器 [英] Handle different view controllers within an application: creating Custom Content View Controllers

查看:32
本文介绍了在应用程序中处理不同的视图控制器:创建自定义内容视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序开发期间,我在支持正确的应用程序架构方面遇到了问题.这对于我目前正在开发的应用程序来说是正确的.特别是,我需要支持如下架构.

During application development I have problems to support the right application architecture. This is true for the application I'm currently developing. In particular, I need to support an architecture like the following.

如您所见,MainViewController 是应用程序的主要控制器,必须在应用程序生命周期内管理不同的控制器.在这种情况下,MainController 是一种集中控制器(其视图的子视图始终位于前面,将其视为菜单视图),它管理应用程序的不同状态(例如 LOGIN_AREA、HOME_AREA、等).

As you can see, the MainViewController is the main controller for the application and has to manage different controllers within the application lifecycle. In this case MainController is a sort of centralized controller (a subview of its view stays always on front, think to it as a menu view) that manages the different states of the application (e.g. LOGIN_AREA, HOME_AREA, etc.).

下面我写了一些实现上述架构的注意事项.

In the following I wrote some consideration to achieve the above architecture.

由于MainViewController 需要总是在其他视图(UIviewControllerX 的视图)前面显示其视图的子视图,我将其设置为rootViewController.

Since MainViewController needs always to display its view's subview in front of other views (UIviewControllerX's views), I set it as the rootViewController.

由于 MainViewController 必须被其他控制器访问(例如 UIViewControllers1),他们需要有一个(弱)引用它.

Since MainViewController has to be accessed by the other controllers (e.g UIViewControllers1), they need to have a (weak) reference it.

MainViewController 有一个公共方法来管理应用程序状态.它存储当前控制器并将选定的控制器视图添加到其视图中作为其子视图.例如:

MainViewController has a public method to manage the application state. It store the current controller and add to its view the selected controller view as its subview. For example:

- (void)manageController
{
   if(currentState == LOGIN_AREA)
   {
      self.currentController = [[[LoginViewController alloc] init] autorelease];
   }

   // other stuff here...

   [self.view addSubView:self.currentController.view];
} 

上述模式似乎有效,但我不太确定它是否正确.实际上,如果 UIViewController(例如 UIViewController1)必须是 UINavigationController,情况会变得复杂.

The above pattern seems to work but I'm not pretty sure if it could be correct. In addiction, the situation gets complicated if a UIViewController (e.g. UIViewController1) has to be a UINavigationController.

我已经阅读了 查看控制器编程指南iOS.特别是,我发现有趣的部分 创建自定义内容视图控制器,但我还没有找到任何示例来创建自定义的控制器.

I've read the View Controller Programming Guide for iOS. In particular, I found interesting the section Creating Custom Content View Controllers but I havent' found any example to create a customized one.

所以,我的问题如下.

您能给我提供实现以下架构的建议吗?或者你能给我一些开发自定义内容视图控制器的技巧吗?

Could you give me suggestions to achieve the following architecture? Or could you give me some tips to develop a custom content view controller?

推荐答案

对于那些有兴趣的人.

要实现类似的机制,有两种不同的方法.

To implement a similar machanism there are two different ways.

1) 实现自定义内容容器.这是一项复杂的任务,但它是可能的.欲了解更多信息,请阅读 writing-high-quality-视图控制器

1) Implement a custom content container. It's a complex task but it's possible. For more info read writing-high-quality-view-controller

2) 使用新的 iOS 5 API.有关更多信息,可以阅读 UIViewController 类参考.

2) Use the new iOS 5 API. For more info it's possible to read Implementing a Container View Controller section in UIViewController Class Reference.

希望有帮助.

这篇关于在应用程序中处理不同的视图控制器:创建自定义内容视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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