欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS开发(stanford2011)笔记第一课MVC架构

程序员文章站 2022-05-08 12:31:29
...

斯坦福大学(stanford university)的iphone开发课由来已久。最近学习了2011年秋季的iphone开发教程。该课程是基于 iOS5的,最新的教程。

这些课程讲的知识点很全面,也讲的很快。于是我做了些笔记,权当督促自己。

 

在第一课中,介绍了iOS的四层结构,和MVC架构。MVC架构在iOS开发中非常普遍的应用架构,其中使用到的target-action和delegate模式很使用,最大程度上降低了各个部分的耦合度。

 

1 、先决条件

熟悉面向对象开发,对这些名词不怵。

Class (description/template for an object)

Instance (manifestation of a class)

Message (sent to object to make it act)

Method (code invoked by a Message)

Instance Variable (object-specific storage)

Superclass/Subclass (Inheritance)

Protocol (non-class-specific methods)

 

2 、苹果的iOS 结构

分成四层架构,分别为Cocoa TouchMediaCore ServicesCore OS

Core OS 是最底层的,提供的都是CAPIMedia 开始,提供的是OBJCAPI ,就是面向对象的API 。我们在开发应用时使用的最多的是cocoa touch 框架。

 

Cocoa Touch

Multi-Touch

Core Motion

View Hierarchy

Localization

Controls

Alerts

Web View

Map Kit

Image Picker

Camera

 

Media

Core Audio

OpenAL

Audio Mixing

Audio Recording

Video Playback

JPEG, PNG, TIFF

PDF

Quartz (2D)

Core Animation

OpenGL ES

 

Core Services

Collections

Address Book

Networking

File Access

SQLite

Core Location

Net Services

Threading

Preferences

URL Utilities

 

Core OS

OSX Kernel

Mach 3.0

BSD

Sockets

Security

Power Management

Keychain Access

Certificates

File System

Bonjour

 

 

3 、在iOS 开发中MVC 架构

 

Model ,应用有些什么数据

 

View ,配合Controller 显示数据,称之controller 的仆人

 

Controller ,如何将model 中的数据显示给用户

 

Controller 能直接向Model 发送消息,也能直接向它的视图发送消息,使用的IBOutlet 对象。但是,ModelView 两个对象则老死不相往来,它们绝对不会相互发送消息。这就是MVC 架构的最基本的原则。

 

View 是否可以主动向Controller 发送消息呢?在实际应用中,这种需求是必须的。

 

iOS 开发中有一种技术,称之为TargetAction 模式,可以来实现这个需求。

首先在Controller 上放置一个target 对象,就是controller 本身,再到View 上设置一个action ,发现执行什么操作的消息。

例如,在一个应用的视图界面上,有一个按钮,执行touch up inside 操作,该操作已经配置了一个action 方法,发送目标是视图控制器。在Controller 类中已有一个此方法的定义,于是在该目标上执行该方法。这就是target-action 模式。

一旦target 改变,执行方法的定义也会改变,controller 操作的结果可以可以反馈到视图界面上。

这种target-action 模式阐述了MVC 架构的特性。

 

苹果文档对于target-action 模式的定义如下:

Target-action is a design pattern in which an object holds the information necessary to send a message to another object when an event occurs. The stored information consists of two items of data: an action selector, which identifies the method to be invoked, and a target, which is the object to receive the message. The message sent when the event occurs is called an action message. Although the target can be any object, even a framework object, it is typically a custom controller that handles the action message in an application-specific way.

 

 

还有一种技术为delegate ,中文通常为翻译成" 委托" 。它也可以实现viewcontroller 的消息交互。

 

View 中很多对象如UITableView 都有一个属性delegateController 会将其自身设置为该属性的值,这样viewdelegate 就是controllerview 对象称之为delegating objectcontroller 对象称之为delegate

View 对象在合适的时间发送一个消息到delegate ,这里就是controller ,该消息通知delegateview 执行了或者将要执行一个操作。controller 执行操作用于响应该消息。

 

简而言之,delegate 的最大价值是让你在一个中心对象上很容易地定义一些对象的行为。另外,Data Source 也属于delegate 的一种。

当然,delegate 作为一种设计模式,在iOS 中还有很多很多用途,如不同的controller 之间的交互,不同视图之间的交互。

 

苹果文档对于delegate 模式的定义如下:

 

Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

 

 

Data Source

 

A data source is almost identical to a delegate. The difference is in the relationship with the delegating object. Instead of being delegated control of the user interface, a data source is delegated control of data. The delegating object, typically a view object such as a table view, holds a reference to its data source and occasionally asks it for the data it should display. A data source, like a delegate, must adopt a protocol and implement at minimum the required methods of that protocol. Data sources are responsible for managing the memory of the model objects they give to the delegating view.

 

 

注,我在delegate模式上绕了很久,至今还是没完全明白。

 

(miki西游 @mikixiyou 原文链接: http://mikixiyou.iteye.com/blog/1586712 )

 

Model 有时候也要向Controller 主动去发送一些消息。这里是通过Notification 来实现的。