对模态和非模态情况重用UIViewController [英] Reusing UIViewController for modal and non-modal situations

查看:129
本文介绍了对模态和非模态情况重用UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIViewController - 我们称之为FormController - 这只是一个编辑对象的形式。我想在两种不同的情况下使用它:

I have a UIViewController — let's call it "FormController" — which is simply a form that edits an object. I want to use it in 2 different situations:


  1. 创建一个新对象 - 使用UINavigationController的 presentModalViewController :

  1. Creating a new object — using using UINavigationController's presentModalViewController: method.

编辑现有对象 - 将视图控制器推送到UINavigationController堆栈,而不是使用对话框方法。 p>

Editing an existing object — push the view controller onto the UINavigationController stack, not using a dialog method.

在模态情况下,我有一个工具栏有取消和完成按钮,而在堆栈情况下,我想只是有UINavigationController提供的导航栏。

There is a slight difference in that in the modal situation I would like to have a toolbar with "Cancel" and "Done" buttons, whereas in the stack situation I would like to just have the navigation bar provided by the UINavigationController.

这将类似于新联系人和编辑联系人屏幕似乎使用相同的视图控制器的联系人应用程序,但新联系人窗体

This would be similar to the Contacts application where the "New Contact" and the "Edit Contact" screens seem to use the same view controller, but the New Contact form is presented modally while the Edit screen is pushed onto the navigation stack.

我的问题是:处理这两种情况的最佳方式是什么,而不必编写2个独立的

我想过创建一个ModalFormController,它通过组合封装了裸露的FormController,并添加了一个工具栏阅读文档中的某处,Apple不建议嵌套视图控制器。

I thought about creating a "ModalFormController" which encapsulates the bare "FormController" through composition and adds a toolbar, but I read somewhere in the docs that Apple doesn't recommend nesting view controllers.

推荐答案

enum ,指定视图控制器的类型。

What I do (sometimes) is set up an enum that specifies the type of the view controller.

例如,您可能有两种类型:编辑类型和添加(新)类型。

For example, you might have two types: an Edit type, and an Add ("new") type.

添加类型通过模态视图控制器实现,而 code>类型推送到现有的导航堆栈。

The Add type is implemented via a modal view controller, while the Edit type is pushed onto an existing navigation stack.

在视图控制器的 -viewDidLoad: ,我只是做一个 switch / case 树,根据上面指定的类型枚举设置标题和其他外观特性。

In the view controller's -viewDidLoad: method, I simply do a switch/case tree that sets up the title and other appearance characteristics depending on the type enumeration specified above.

这是一个很好的事情,很容易添加一个新的类型。缺点是,用于处理这个枚举的条件树可能很快复杂,这取决于类型的不同。

The nice thing about this is that it is easy to add a new type. The downside is that the conditional tree for handing this enumeration can get complicated quickly, depending on how different the types are.

但是 switch / case 树可以更容易管理。

But the switch/case tree makes it a lot easier to manage.

所以,这取决于你试图做的两种类型。但它绝对是可行的。

So, it depends on what you're trying to do with the two types. But it's definitely doable.

这篇关于对模态和非模态情况重用UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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