为 MVC 设计模式组织 iOS 项目 [英] Organizing iOS project for MVC design pattern

查看:22
本文介绍了为 MVC 设计模式组织 iOS 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发适用于 iPhone 的多视图应用程序,目前我的视图 (VIEW) 设置和它们的转换(控制器?)运行良好.现在我想为实际程序数据(模型)添加对象.

I'm working on a multiview app for iPhone and currently have my views (VIEW) set up and their transitions (CONTROLLER?) working nicely. Now I'd like to add objects for the actual program data (MODEL).

我的问题是:我应该如何构建数据以符合模型视图控制器 (MVC) 设计模式?我知道我应该创建单独的类来实现我的数据结构,并且我的控制器类可以从视图向它们传递消息,但是还有其他组织方面的注意事项我应该检查吗?尤其是 Cocoa Touch、Xcode 或 iOS 特有的那些?

My question is: How should I structure my data to adhere to the Model View Controller (MVC) design pattern? 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?

其他细节: 播放预先录制的和可能是用户生成的音频也很重要.我知道这些是模型元素,但它们与V"和C"究竟有什么关系,我仍然有点模糊.我想当用户操作需要音频播放时,控制器应该将消息传递给模型以准备适当的声音,但是播放的调节究竟应该在哪里进行?在与我想象的 ViewController 分开的PlayerController"中?

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. 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? In a "PlayerController" separate from the ViewController I imagine?

非常感谢并原谅我的 MVC noobery.

Many thanks and pardon my MVC noobery.

推荐答案

Caleb 很好地介绍和概述了如何思考问题.在您的特定情况下,以下是您可能会给出描述的一些部分:

Caleb gives a good introduction and overview of how to think about the problem. In your particular case, here are some of the pieces you would might have given your description:

  • Clip (M) - 负责保存实际的音频数据.它会知道如何解释数据并提供有关它的信息,但实际上不会播放任何内容.

  • Clip (M) - Responsible for holding the actual audio data. It would know how to interpret the data and given information about it, but it wouldn't actually play anything.

播放器 (V) - 实际上在扬声器上播放剪辑.是的,这是 MVC 中的一种视图.音频只是另一种演示.也就是说,您永远不会称它为PlayerView",因为这表明它是 UIView 的子类.

Player (V) - Actually plays a clip on the speakers. Yes, that's a kind of view in 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 (V) - 播放器的屏幕表示.对 Clips 一无所知.

PlayerView (V) - A screen representation of the Player. Knows nothing about Clips.

ClipManager (C) - 一个对象,用于跟踪系统中的所有剪辑并管理从网络中获取它们、将它们添加到缓存中以及将它们删除等.

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.

PlayerViewController (C) - 从 ClipManager 检索 Clip,并协调 Player 和 PlayerView 以显示和播放它,以及任何其他 UI 元素(如后退按钮"等).

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).

这只是一个示例,说明您如何将其分解为一些理论音频播放器应用程序.有很多正确的 MVC 方法可以做到这一点,但这是一种思考方式.

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 设计模式组织 iOS 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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