您的位置:首页 > 移动开发 > IOS开发

iOS开发中的设计模式----MVC模式

2017-02-21 13:57 387 查看
MVC在开发中应该是应用最广泛的一种设计模式了,而且在面试中也是经常会被问到,但很多人都觉得MVC太普遍了可能就会忽略掉,当被问及时就不知道从何说起了,其实本人就遇到过这种尴尬的局面。。。

好了,废话不多说了,先来看看什么是MVC

MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式:

Model(模型)表示应用程序核心(比如数据库记录列表)。

View(视图)显示数据(数据库记录)。

Controller(控制器)处理输入(写入数据库记录)。

Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。

  通常模型对象负责在数据库中存取数据。

View(视图)是应用程序中处理数据显示的部分。

  通常视图是依据模型数据创建的。

Controller(控制器)是应用程序中处理用户交互的部分。

  通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。

MVC 分层有助于管理复杂的应用程序,因为您可以在一个时间内专门关注一个方面。例如,您可以在不依赖业务逻辑的情况下专注于视图设计。同时也让应用程序的测试更加容易。

MVC 分层同时也简化了分组开发。不同的开发人员可同时开发视图、控制器逻辑和业务逻辑。

但在iOS开发中,即时使用MVC模式,程序也并没有达到理想的解耦和,因为iOS中的view必须依赖于controller才能展示出来,因此并不是完美的MVC模式,根据自己对MVC的理解,写了一个简单的Demo,希望大家多多指正,共同进步。

//model文件
#import <Foundation/Foundation.h>
@interface BookModel : NSObject
@property (nonatomic, strong)NSString * bookName; //书名
@property (nonatomic, strong)NSString * desp;     //描述
@property (nonatomic, assign)double price;        //价格
- (instancetype)initWithName:(NSString *)name price:(double)price desp:(NSString *)desp;
@end

#import "BookModel.h"
@implementation BookModel

- (instancetype)initWithName:(NSString *)name price:(double)price desp:(NSString *)desp{
self.bookName = name;
self.price = price;
self.desp = desp;

return self;
}
@end

//view文件
#import <UIKit/UIKit.h>
@class BookModel;
@interface BookCell : UITableViewCell

@property (nonatomic, strong)BookModel * model;
@end

#import "BookCell.h"
#import "BookModel.h"

@implementation BookCell
{
UILabel * name;     //名称
UILabel * price;    //价格
UILabel * desp;     //描述
}

//初始化cell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

[self initLayout];
}
return self;

}
//视图初始化
- (void)initLayout{

UIView * backView = [[UIView alloc] initWithFrame:self.bounds];
backView.backgroundColor = [UIColor whiteColor];
[self addSubview:backView];
//名称
name = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 50, 30)];
name.textColor = [UIColor blackColor];
[backView addSubview:name];

//价格
price = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 80, 30)];
price.textColor = [UIColor redColor];
[backView addSubview:price];

//描述
desp = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 200, 30)];
desp.textColor = [UIColor grayColor];
[backView addSubview:desp];

}
//设置model
- (void)setModel:(BookModel *)model {
name.text = model.bookName;
price.text = [NSString stringWithFormat:@"%.2lf元",model.price];
desp.text = model.desp;

}

@end

//controller文件
#import "ViewController.h"
#import "Singleton.h"
#import "BookModel.h"
#import "BookCell.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView * tableView;

@end
@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self mvcTest];

}

- (void)mvcTest{

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height - 20)];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return 5;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

BookCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[BookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
BookModel * model = [[BookModel alloc] initWithName:[NSString stringWithFormat:@"我是%ld", indexPath.row] price:indexPath.row*2.5 desp:[NSString stringWithFormat:@"这是%ld",indexPath.row]];
cell.model = model;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
@end
以下是运行结果,比较low的界面



本人从事iOS开发不久,希望大家能多多指导,共同进步!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息