MVC模式:M(Model)、V(View)、C(Controller)。
引出:
在开发过程中,处理发送请求,业务逻辑处理以及访问数据,这三个功能我们是可以放到一起的,但是如果放在一起,代码便会很臃肿,不利于维护,于是便出现了代码分层思想:把代码按照功能分为三层,即模型层(Model)、显示层(View)、控制层(Controller),这种代码的组织架构就叫MVC模式。
MVC介绍:
Model(模型对象):
模型对象封装了应用程序的数据,并定义操控和处理该数据的逻辑和运算。模型对象(数据)要发生更改,通知控制器对象,控制器对象更新相应的视图对象,这就是一套完整的流程。
View(视图对象):
视图对象是应用程序中用户可以看见的对象。在iOS应用程序开发中,所有的控件、窗口等都继承自 UIView,对应MVC中的V。UIView及其子类主要负责UI的实现,而UIView所产生的事件都可以采用委托的方式,交给UIViewController实现。
Controller(控制器对象):
控制器对象解释在视图对象中进行的用户操作,并将新的或更改过的数据传达给模型对象。Controller在这个MVC中扮演转义数据的角色(因为呈现在View中的数据可能是便于用户阅读的文字或是数字,而存储在Model中的数据可能是比较高效但不便于直接阅读的二进制或是其他存储方式,Controller可以承担转化这两种数据形式的职责)。模型对象更改时,一个控制器对象会将新的模型数据传达给视图对象,以便视图对象可以显示它。对于不同的UIView,有相应的UIViewController,对应MVC中的C。例如在iOS上常用的UITableView,它所对应的Controller就是UITableViewController。
示例:
M:
// MyModel.h
// MVCStudying
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MyModel : NSObject
@property (nonatomic, strong) NSMutableArray *keyStringArray;
- (void)myModelInit;
- (void)myModelAdd:(NSString*)add;
@end
NS_ASSUME_NONNULL_END
// MyModel.m
// MVCStudying
#import "MyModel.h"
@implementation MyModel
- (void)myModelInit {
_keyStringArray = [[NSMutableArray alloc] init];
[_keyStringArray addObject:@"keyFirst"];
}
- (void)myModelAdd:(NSString *)add {
[_keyStringArray addObject:add];
}
@end
V:
// MyView.h
// MVCStudying
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MyView : UIView
@property (nonatomic, strong) UITextField *myTextField;
@property (nonatomic, strong) UIButton *myButton;
@property (nonatomic, strong) UIButton *addButton;
@end
NS_ASSUME_NONNULL_END
// MyView.m
// MVCStudying
#import "MyView.h"
#define selfWeidth self.bounds.size.width
#define selfHeight self.bounds.size.height
@implementation MyView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor lightGrayColor];
_myTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, selfWeidth - 100, 50)];
_myTextField.placeholder = @"MyTextHolder";
_myTextField.backgroundColor = [UIColor whiteColor];
[self addSubview:_myTextField];
_myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_myButton.frame = CGRectMake(100, 300, selfWeidth - 200, 50);
[_myButton setTitle:@"MyButton" forState:UIControlStateNormal];
[_myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_myButton.backgroundColor = [UIColor whiteColor];
[self addSubview:_myButton];
_addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_addButton.frame = CGRectMake(100, 400, selfWeidth - 200, 50);
[_addButton setTitle:@"AddButton" forState:UIControlStateNormal];
[_addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_addButton.backgroundColor = [UIColor whiteColor];
[self addSubview:_addButton];
return self;
}
@end
C:
// MyViewController.h
// MVCStudying
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MyViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
// MyViewController.m
// MVCStudying
#import "MyViewController.h"
#import "MyModel.h"
#import "MyView.h"
@interface MyViewController()
@property (nonatomic, assign) NSInteger flag;
@property (nonatomic, strong) MyView *myView;
@property (nonatomic, strong) MyModel *myModel;
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
_myView = [[MyView alloc] initWithFrame:self.view.frame];
[_myView.myButton addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];
[_myView.addButton addTarget:self action:@selector(pressAdd) forControlEvents:UIControlEventTouchUpInside];
_myModel = [[MyModel alloc] init];
[_myModel myModelInit];
[self.view addSubview:_myView];
}
- (void)pressLogin {
_flag = 0;
for (NSString *key in _myModel.keyStringArray) {
if ([key isEqualToString:_myView.myTextField.text]) {
_flag = 1;
}
}
if (_flag == 1) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Top Tip" message:@"Correct key" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"Sure" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:sure];
[self presentViewController:alert animated:NO completion:nil];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Top Tip" message:@"Wrong key" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancel];
[self presentViewController:alert animated:NO completion:nil];
}
}
- (void)pressAdd {
_flag = 0;
for (NSString *key in _myModel.keyStringArray) {
if ([key isEqualToString:_myView.myTextField.text]) {
_flag = 1;
}
}
if (_flag == 1) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Top Tip" message:@"Fail" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:sure];
[self presentViewController:alert animated:NO completion:nil];
} else {
[_myModel myModelAdd:_myView.myTextField.text];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Top Tip" message:@"Success" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Sure" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancel];
[self presentViewController:alert animated:NO completion:nil];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[_myView.myTextField resignFirstResponder];
}
@end
以上是一个极其简单的例子,Model部分封存数据(keyStringArray),并提供数据初始化与操作方法。View部分展示TextField和两个按钮。
用户通过按钮可以实现对数据的添加等操作。
下面放上一个小Demo的git链接https://github.com/BillyMiracle/MVCDemo.git。