简体   繁体   English

为MVC设计模式组织iOS项目

[英]Organizing iOS project for MVC design pattern

I'm working on a multiview app for iPhone and currently have my views (VIEW) set up and their transitions (CONTROLLER?) working nicely. 我正在研究iPhone的多视图应用程序,目前我的视图(VIEW)设置和转换(CONTROLLER?)工作得很好。 Now I'd like to add objects for the actual program data (MODEL). 现在我想为实际的程序数据(MODEL)添加对象。

My question is: How should I structure my data to adhere to the Model View Controller (MVC) design pattern? 我的问题是:我应该如何构建我的数据以遵守模型视图控制器(MVC)设计模式? I know I should create separate classes to implement my data structures and that my controller classes can pass messages to them from the view, but are there any other organizational considerations I should examine? 我知道我应该创建单独的类来实现我的数据结构,并且我的控制器类可以从视图中将消息传递给它们,但是我应该检查其他任何组织注意事项吗? Especially those particular to Cocoa Touch, Xcode, or iOS? 特别是Cocoa Touch,Xcode或iOS特有的那些?

Other particulars: Playback of pre-recorded and perhaps user-generated audio will also be essential. 其他细节:播放预先录制的音频和用户生成的音频也是必不可少的。 I know these are model elements, but how exactly they relate to the "V" and the "C" I'm still a bit fuzzy on. 我知道这些是模型元素,但它们与“V”和“C”的确切关系我仍然有点模糊。 I suppose when a user action requires audio playback, the CONTROLLER should pass a message to the MODEL to ready the appropriate sounds, but where exactly should regulation of the playback live? 我想当用户操作需要音频播放时,CONTROLLER应该将消息传递给MODEL以准备好适当的声音,但是应该在哪里准确地调节播放? In a "PlayerController" separate from the ViewController I imagine? 在一个与我想象的ViewController分开的“PlayerController”中?

Many thanks and pardon my MVC noobery. 非常感谢和赦免我的MVC noobery。

Caleb gives a good introduction and overview of how to think about the problem. Caleb对如何思考问题给出了很好的介绍和概述。 In your particular case, here are some of the pieces you would might have given your description: 在您的特定情况下,以下是您可能会给出描述的部分内容:

  • Clip (M) - Responsible for holding the actual audio data. Clip(M) - 负责保存实际的音频数据。 It would know how to interpret the data and given information about it, but it wouldn't actually play anything. 它会知道如何解释数据并提供有关它的信息,但它实际上不会发挥任何作用。

  • Player (V) - Actually plays a clip on the speakers. 播放器(V) - 实际上在扬声器上播放剪辑。 Yes, that's a kind of view in MVC. 是的,这是MVC中的一种观点 Audio is just another kind of presentation. 音频只是另一种表现形式。 That said, you'd never call it "PlayerView" because that would suggest it were a subclass of UIView. 也就是说,你永远不会把它称为“PlayerView”,因为这表明它是UIView的子类。

  • PlayerView (V) - A screen representation of the Player. PlayerView(V) - 播放器的屏幕表示。 Knows nothing about Clips. 什么都不知道剪辑。

  • ClipManager (C) - An object that would keep track of all the clips in the system and manage fetching them from the network, adding and removing them to caches, etc. ClipManager(C) - 一个跟踪系统中所有剪辑并管理从网络中获取它们,添加和删除它们到缓存等的对象。

  • PlayerViewController (C) - Retrieves a Clip from the ClipManager, and coordinates a Player and a PlayerView to display and play it, as well as any other UI elements (like a "back button" or the like). PlayerViewController(C) - 从ClipManager中检索剪辑,并协调播放器和PlayerView以显示和播放它,以及任何其他UI元素(如“后退按钮”等)。

This is just an example of how you might break it down for some theoretical audio player app. 这只是一个如何分解一些理论音频播放器应用程序的例子。 There are many correct MVC ways to do it, but this is one way to think about it. 有许多正确的MVC方法可以做到这一点,但这是一种思考它的方法。

Lord John Worfin (and, I'm sure, someone before him) said: "Character is what you are in the dark." John Worfin勋爵(而且,我确定,有人在他面前)说:“性格就是你在黑暗中的样子。” Well, a model is what an application is when nobody is looking -- it's the data and logic that defines how the app behaves regardless of how it's presented on screen. 好吧,模型就是当没人看的时候应用程序是什么 - 它是定义应用程序行为的数据和逻辑,无论它在屏幕上呈现的方式如何。

Imagine that you decide to add a command-line interface to your application. 想象一下,您决定在应用程序中添加命令行界面。 You'd still want to use the same structures for managing your data, and your logic for sorting, sifting, and calculating based on the data should still be the same too. 您仍然希望使用相同的结构来管理数据,并且基于数据进行排序,筛选和计算的逻辑也应该相同。 The code in your app that remains important/useful no matter how the user sees or interacts with the app is the model. 无论用户如何看到应用程序或与应用程序交互,应用程序中的代码仍然非常重要/有用。

A model can be very simple and made up entirely of standard objects. 模型可以非常简单,完全由标准对象组成。 iOS apps are often more about retrieving, storing, and displaying data than they are about crunching numbers, so it's not unusual to have a model that's just an array of dictionaries, or a hierarchy of dictionaries that's several levels deep. iOS应用程序通常更多地是关于检索,存储和显示数据,而不是关于处理数字,所以拥有一个只是一个字典数组的模型或者几个级别的字典层次结构并不罕见。 If you look at Core Data's NSManagedObject class, it's similar in many respects to NSMutableDictionary. 如果你看一下Core Data的NSManagedObject类,它在许多方面与NSMutableDictionary类似。 So, don't be afraid to use standard objects if they're appropriate. 所以,如果它们合适,不要害怕使用标准物体。

That said, you can certainly also create your own model objects, and that's useful if you have certain requirements that you want to enforce on your data, or if you want to be able to derive information from the data. 也就是说,您当然也可以创建自己的模型对象,如果您希望对数据执行某些要求,或者您希望能够从数据中获取信息,那么这些对象非常有用。

Beginners often wonder how each controller gets access to the model. 初学者经常想知道每个控制器如何访问模型。 Some folks advocate using the singleton pattern for this, mainly because it provides a single, shared, globally accessible object. 有些人主张使用单例模式,主要是因为它提供了一个单独的,共享的,全局可访问的对象。 I don't recommend this. 我不推荐这个。 Instead, have some high-level object in your app such as the app delegate create/load the model (which will likely be a graph of many individual objects) and give a pointer to the model to any view controllers that need it. 相反,在应用程序中有一些高级对象,例如app delegate创建/加载模型(可能是许多单个对象的图形),并将模型指针指向需要它的任何视图控制器。 If those controllers in turn create other view controllers, they can again provide a pointer to the model (or part of it) to their child controllers. 如果这些控制器依次创建其他视图控制器,则它们可以再次向其子控制器提供指向模型(或其一部分)的指针。

I hope that helps. 我希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM