MVC间的通信(附代码)

来源:互联网 发布:js中的选择器 编辑:程序博客网 时间:2024/06/06 21:06

首先我们解释一下MVC分别是什么
M(Model)
应用程序的核心,用于处理应用程序数据逻辑部分,例如JSON解析的模型类
V(View)
处理应用程序数据的显示,也是是视图层
通常依据Model创建
C(Controller)
处理用户的交互,控制器层
从视图读取数据,控制用户输入,并向模型发送数据
下面根据斯坦福的一张截图课件我们来分析一下,MVC间的通信
这里写图片描述
总结

C对M:
通过API
参考代码:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    Message *m  = self.messageArr [indexPath.row];    return [m height]+105;}

C对V:
通过outlet
参考代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];    cell.message = self.messageArr[indexPath.row];    return cell;}

V对C:
Target-action:为View中的某个按钮添加一个监听事件,当按钮被点击时就执行相应的处理。
Delegate:比如tableview中的

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

Datasource:比如tableview中的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;  

M对C:
M对C通信可以采用Notification&KVO,可以参考另一篇微博的Demo来理解
http://blog.csdn.net/lee727n/article/details/71513609

0 0
原创粉丝点击