OC项目之使用MVC架构模式进行开发
我们在开发一个OC项目时,常常会用到MVC架构模式。
Model层是模型层,负责提供页面上的数据。
举个🌰说明:
RFTestModel.h
#import "RFBaseModel.h" NS_ASSUME_NONNULL_BEGIN @interface RFTestModel : RFBaseModel @end NS_ASSUME_NONNULL_END
RFTestModel.m
#import "RFTestModel.h" @implementation RFTestModel @end
View层是视图层,负责绘制页面视图。
RFTestView.h
#import "RFBaseView.h" #import "RFTableView.h" #import "RFTestModel.h" NS_ASSUME_NONNULL_BEGIN @interface RFTestView : RFTableView @end typedef void (^mapBtnBlock)(void); @interface RFTestHeaderView : RFBaseView @property (nonatomic, copy) mapBtnBlock mapBtnClick; @end NS_ASSUME_NONNULL_END
其中,Block可以定义在视图层的声明文件:
typedef void (^mapBtnBlock)(void); @interface RFTestHeaderView : RFBaseView @property (nonatomic, copy) mapBtnBlock mapBtnClick; @end
上述代码定义了一个名为mapBtnBlock的Block,并使用它定义了mapBtnClick作为视图的属性。
RFTestView.m
#import "RFTestView.h" @implementation RFTestView @end @interface RFTestHeaderView () @property (nonatomic, strong) RFButton *mapBtn; @end @implementation RFTestHeaderView -(instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.backgroundColor = RF_RGB_COLOR(0, 0, 255); UILabel* label = [[UILabel alloc]init]; [self addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.mas_centerX); make.top.equalTo(self.mas_top).offset(100); make.size.mas_equalTo(CGSizeMake(200, 50)); }]; label.backgroundColor = [UIColor greenColor]; UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom]; [self addSubview:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(label); make.top.equalTo(label.mas_bottom).offset(100); make.size.mas_equalTo(CGSizeMake(200,50)); }]; btn.backgroundColor = [UIColor yellowColor]; [btn setTitle:@"更新约束" forState:(UIControlStateNormal)]; [btn addTarget:self action:(@selector(pressBtn:)) forControlEvents:(UIControlEventTouchUpInside)]; [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)]; UIView* view = [[UIView alloc]init]; [view setBackgroundColor:[UIColor redColor]]; [self addSubview:view]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.mas_centerX); make.top.equalTo(btn.mas_bottom).offset(30); make.size.mas_equalTo(CGSizeMake(300, 300)); }]; _mapBtn = [RFButton buttonWithType:UIButtonTypeCustom]; [self addSubview:_mapBtn]; [_mapBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.mas_centerY); make.right.equalTo(self.mas_right).offset(-15.0f); make.height.mas_equalTo(200.0f); [_mapBtn setTitle:@"打印机分布" forState:UIControlStateNormal]; [_mapBtn setTitleColor:RF_RGB_COLOR(0, 0, 0) forState:UIControlStateNormal]; [[_mapBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) { if (self.mapBtn){ self.mapBtnClick(); } }]; }]; } return self; } -(void)pressBtn:(UIButton*)btn{ [btn mas_updateConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(300,100)); }]; } @end
上述代码自定义initWithFrame函数,使用Masonry,新建组件、加入到父视图、生成组件约束。
上述代码添加了label、button、view和button。第二个按钮则增加了事件,使用了ReactiveObjC,当按钮被点击时,会调用mapBtnClick块。
可以在组件上增加事件,并触发对应的函数,进行视图的修改。
RFTestViewController.h
#import "RFBaseViewController.h" NS_ASSUME_NONNULL_BEGIN @interface RFTestViewController : RFBaseViewController @end NS_ASSUME_NONNULL_END
RFTestViewController.m
#import "RFTestView.h" #import "RFTestViewController.h" #import "RFPrinterMapViewController.h" @interface RFTestViewController () @property (nonatomic, strong) RFTestHeaderView *headerView; @property (nonatomic, strong) RFButton *mapBtn; @end @implementation RFTestViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"会议助手"; [self initUI]; } - (void)initUI{ _headerView = [[RFTestHeaderView alloc] initWithFrame:CGRectZero]; [self.view addSubview:_headerView]; [_headerView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.equalTo(self.view); make.height.mas_equalTo(400.0f); }]; RF_WEAKILY(self); _headerView.mapBtnClick = ^{ RF_STRONGIFY(self); RFPrinterMapViewController *vc = [[RFPrinterMapViewController alloc]init]; [self.navigationController pushViewController:vc animated:YES]; }; }
ViewController是页面的入口,viewDidLoad作为页面加载的hook函数被调用。在这个函数中可以进行UI的初始化。
构建View并进行View的摆放。同时,视图内触发的Block会在这里回调处理,可以跳转到其他视图的ViewController。
在Block外需要进行weakify,进入Block需要进行strongify。