Model-View-Controller
MVC separates the way an onscreen object looks from the way it behaves. An onscreen button (the view) has no intrinsic meaning. It’s just a button that users can push.That view’s controller acts as an intermediary. It connects user interactions such as button taps to targeted methods in your application, which is the model.The application supplies and stores meaningful data and responds to interactions such as these button taps by producing some sort of useful result.
Each MVC element works separately.You might swap out a push button with, for example, a toggle switch without changing your model or controller.The program con- tinues to work as before, but the GUI now has a different look. Alternatively, you might leave the interface as is and change your application where a button triggers a different kind of response in your model. Separating these elements enables you to build main- tainable program components that can be updated independently.
The MVC paradigm on the iPhone breaks down into the following categories:
@ View. View components are provided by children of the UIView class and by its associated (and somewhat misnamed) UIViewController class.
@Controller. The controller behavior is implemented through three key technolo- gies: delegation, target-action, and notification.
@Model. Model methods supply data through protocols such as data sourcing and meaning by implementing callback methods triggered by the controller.
Together these three elements form the backbone of the MVC programming para- digm. Let’s look at each of these elements of the iPhone MVC design pattern in a bit more detail.The following sections introduce each element and its supporting classes.
Model-View-Controller
MVC 分离了屏幕上显示的对象和它的行为。屏幕上的一个按钮(view)没有实际的意义,它只是一个用户可以点击的按钮。view's controller(视图的控制器)扮演一个中间人的角色。model连接用户交互,比如你的程序中按钮点击到目标方法。应用程序提供并存储有意义的数据并响应用户交互比如按钮点击产生的一系列的用户数据。
MVC的每个元素都是分开工作的。你可以用一个toggle switch 替换 push button 而不用改变你的model 和 controller.程序仍和以前一样运行,只是在界面显示的有些不一样。或者你可以不改变界面,而是改变你程序中model对应的按钮的响应。把这些元素分离开来可以让你创建具有可维护性的,迅速更新的程序元素。
iPhone中的MVC范例类目:
@ View view元素是由UIView类和与它相关的UIViewControler 类 及他们的子类提供。
@Conreoller controller行为的实现是通过三大关键技术:delegation,target-action,notification 。
@Model model方法通过协议提供数据,例如通过实现controller触发的回调方法提供数据来源和意义。