为什么使用 ViewController 来管理 ViewController 而不仅仅是 ApplicationDelegate
如果我有一堆只处理单个视图的 ViewController,或者即使我的 ViewController 要处理多个视图,为什么我应该使用另一个 ViewController 来管理其他 ViewController?为什么我不直接在 ApplicationDelegate 级别更改 ViewController?
也许我对 ViewController 的思考方式是错误的?我习惯于使用 Ruby/.NET 以 MVC 模式进行编写。例如,如果我正在使用小部件,我可能会有一个 WidgetController 和一个列表视图,以及 WidgetController 的详细信息视图。
类似的 iPhone MVC 结构是什么?我假设 WidgetController 会继承 ViewController 的子类,并且根据我想要如何查看小部件数据,我会有几个不同的视图。然后,当我想要处理 Wodgits 时,我会创建一个 WodgitController 及其关联的视图,并将窗口的子视图与新的 Wodgit ViewController 交换。
我不明白使用 RootViewController 来控制我的控制器能给我带来什么。价值在哪里?我缺少什么?
If I have a bunch of ViewControllers that are only ever going to deal with a single view, or even if my ViewController is going to deal with multiple views, why should I use another ViewControllers to manage the other ViewControllers? Why wouldn't I just change out ViewControllers at the ApplicationDelegate level?
Maybe I'm thinking about ViewController the wrong way? I'm used to writing in the MVC pattern with Ruby/.NET. For an example, if I were working with widgets I'd probably have a WidgetController and a List view, and a Detail view for the WidgetController.
What is the analogous iPhone MVC construction? I'd assume the WidgetController would subclass the ViewController and I'd have a couple different views depending on how I wanted to look at the widget data. Then, when I wanted to deal with Wodgits I'd make a WodgitController with its associated views and swap the window's subview out with the new Wodgit ViewController.
I don't see what having a RootViewController to control my controllers buys me. Where is the value? What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将重点关注一个方面:划分是一种非常有用的设计原则。如果您将关注点分开并将事物放在它们“所属”的位置,那么它们将更容易查找、维护、构建和协作-选择新的目的。
考虑一下:
你家里的所有管道都连接到同一个地方。您是否在水槽中小便,或者您是否使用为此目的设计的固定装置?
对于您的应用程序,应用程序委托负责响应应用程序事件并管理通用应用程序资源。根视图控制器负责管理视图和视图控制器。作为视图控制器,它具有导航控制、呈现模式视图的能力、充当委托等方面的特殊能力。虽然您可以创建视图控制器并在应用程序委托中完全操作它,但任务将呈指数级增长当您管理的视图数量成倍增加时,就会变得更加困难,特别是如果您希望成为文本字段委托、数据源等。另一方面,很容易将根视图控制器拆分为它自己的类。成本非常低。
距离厕所只有一步之遥,为什么还要在水槽里撒尿呢?
I'll focus on one aspect: Compartmentalization is a very useful design principal. If you separate concerns and put things where they "belong", they will be easier to find, maintain, build upon, and co-opt for new purposes.
Consider this:
All the pipes in your house go to the same place. Do you urinate in your sink, or do you use the fixture designed for that purpose?
In the case of your app, the application delegate is responsible for responding to application events and managing universal application resources. The root view controller is responsible for managing your views and view controllers. As a view controller it has special abilities to do so with navigation control, the ability to present modal views, act as a delegate, etc. While you could create a view controller and manipulate it completely in the application delegate, the task will become exponentially more difficult as you multiply the number of views you are managing, especially if you wish to be a text field delegate, data source, etc. On the other hand, it is easy to split the root view controller into its own class. There is very little cost.
Why pee in the sink when the toilet is a step away?