ios中的MVC设计模式

1、MVC概念ios

MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范。MVC的目的是将M和V的实现代码分离,从而使同一个程序可使用不一样的表现形式。C存在的目的则是确保M和V的同步,一旦M改变,V应该同步更新。ide

 2、MVC间通讯设计

一、Model和View永远不能相互通讯,只能经过Controller传递。事件

二、Controller能够直接与Model对话(读写调用Model),Model经过Notification和KVO机制与Controller间接通讯。get

三、Controller能够直接与View对话(经过outlet,直接操做View,outlet直接对应到View中的控件),View经过 action向Controller报告事件的发生(如用户Touch我了)。Controller是View的直接数据源(数据极可能是 Controller从Model中取得的)。View能够经过delegate与Controller进行通讯。同步

下面根据ios的MVC示意图再补充说说:it

MVC示意图

1.  图中,绿色的箭头表示直接引用。 对View 的直接引用体如今 IBOutlet 上。 当引用一个View 时,好比Button。 须要在ViewControllerio

中声明一个  IBOutlet  UIButton * btn;软件

2. 而后,咱们看View 是怎么向 Controller 通讯的。对于这个,  iOS 有三种常见的模式:model

      设置View对应的Action Target。如设置UIButton的Touch up inside的Action Target。

      设置View的Delegate,如UIAlertViewDelegate, UIActionSheetDelegate,UITextFieldDelegate等。

      设置View的data source, 如UITableViewDataSource。

      经过以上三种模式,View既能向Controller通讯,又无需知道具体的Controller是谁,这样,View 就与Controller解耦了。

相关文章
相关标签/搜索