MVC是什么
MVC是Model、VIew、Controller,即模型、视图、控制器,用数据(模型)、界面显示(视图)、业务逻辑(控制器)分离的方法组织代码,是一种软件设计的规范。
模型Model
模型负责各个功能的实现(如登录、增加、删除功能)。
视图View
视图负责页面的显示和与用户的交互。在iOS应用程序开发中,所有的控件、窗口等都继承自 UIView,对应MVC中的V。UIView及其子类主要负责UI的实现,而UIView所产生的事件都可以采用委托的方式,交给UIViewController实现。
控制器Controller
控制器负责将视图与模型一一对应起来,即视图和模型是不直接联系的,控制器充当其调节器,成为一个应用程序的CPU。
MVC
MVC模式能够完成各司其职的任务模式,由于降低了各个环节的耦合性,大大优化Controller的代码量,当程序调试时,如果某一个功能没有按照既定的模式工作,可以很方便的定位到到底是Controller还是View还是Model出了问题。
ps:
1.Model和View只能通过Controller传递 , 永远不能相互通信!
2.Controller可以直接与Model对话 ( 读写调用Model ) , Model通过Notification和KVO机制与Controller间接通信。
3.Controller可以直接与View对话 , 通过outlet , 直接操作View , outlet直接对应到View中的控件 , View通过action向Controller报告事件的发生(如用户进行了Touch操作)。Controller是View的直接数据源 ( 数据一般是Controller从Model中取得并经过加工的 ) Controller是View的代理 ( delegate ) , 以同步View与Controller。
登陆注册demo
1、新建登陆和注册文件夹,在各自文件夹中新建Model、View、 Controller三个文件夹
Model中新建一个名为LandingModel的类,继承自NSObject(以登陆界面为例)
View中新建一个名为LandingView的类,继承自UIView
Controller中新建一个名为LandingController的类,继承自UIViewController
Model
//LandingModel.h
@interface LandingModel : NSObject
@property (nonatomic, strong) NSMutableArray *userArray;
@property (nonatomic, strong) NSMutableArray *passwordArray;
- (void)landingMassageInit;
@end
//LandingModel.m
#import "LandingModel.h"
@implementation LandingModel
- (void)landingMassageInit {
_userArray = [[NSMutableArray alloc] init];
_passwordArray = [[NSMutableArray alloc] init];
[_userArray addObject:@"123"];
[_passwordArray addObject:@"123"];
}
@end
View
//LandingView.h
@interface LandingView : UIView
@property (nonatomic,strong) UITextField *userTextField;
@property (nonatomic,strong) UITextField *passwordTextField;
@property (nonatomic,strong) UIButton *landingButton;
@property (nonatomic,strong) UIButton *registerButton;
@end
//LandingView.m
#import "LandingView.h"
@implementation LandingView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor whiteColor];
_userTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 175, 60)];
_userTextField.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.5];
_userTextField.placeholder = @" 输入用户名";
[self addSubview:_userTextField];
_passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 175, 60)];
_passwordTextField.placeholder = @" 输入密码";
_passwordTextField.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.5];
[self addSubview:_passwordTextField];
_landingButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_landingButton setTitle:@"登陆" forState:UIControlStateNormal];
[_landingButton setFrame:CGRectMake(100, 400, 75, 30)];
[self addSubview:_landingButton];
_registerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_registerButton setTitle:@"注册" forState:UIControlStateNormal];
[_registerButton setFrame:CGRectMake(200, 400, 75, 30)];
[self addSubview:_registerButton];
return self;
}
@end
Controller
//LandingController.h
#import "LandingModel.h"
#import "LandingView.h"
#import "RegisterController.h"
@interface LandingController : UIViewController
@property(nonatomic,strong) LandingView *landingView;
@property(nonatomic,strong) LandingModel *landingModel;
- (void)passName:(NSString *)name passPassword:(NSString *)password;
@end
//LandingController.m
#import "LandingController.h"
@interface LandingController ()
@end
@implementation LandingController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:)name:@"tongzhi" object:nil];
_landingView = [[LandingView alloc] init];
_landingView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[_landingView.registerButton addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
[_landingView.landingButton addTarget:self action:@selector(pressLanding) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_landingView];
_landingModel = [[LandingModel alloc] init];
[_landingModel landingMassageInit];
}
- (void)pressRegister {
RegisterController* registerViewController = [[RegisterController alloc]init];
registerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
registerViewController.existUserArray = _landingModel.userArray;
registerViewController.registerDelegate = self;
[self presentViewController:registerViewController animated:YES completion:nil];
}
- (void)pressLanding {
BOOL flag = NO;
for (int i = 0; i < _landingModel.passwordArray.count; i++) {
if ([_landingView.userTextField.text isEqualToString:_landingModel.userArray[i]] && [_landingView.passwordTextField.text isEqualToString:_landingModel.passwordArray[i]]) {
flag = YES;
break;
}
}
if (flag == YES) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:sure];
[self presentViewController:alert animated:YES completion:nil];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"帐号或密码错误!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction:sure];
[self presentViewController:alert animated:YES completion:nil];
}
}
- (void)passName:(NSString *)name passPassword:(NSString *)password {
[_landingModel.userArray addObject:name];
[_landingModel.passwordArray addObject:password];
}
@end
注册界面的View和Model和登陆界面同理,Controller中增加了协议传值将新注册的账号密码传入到登陆界面,还有对登陆中用户名的查重
运行效果
登陆失败
注册
登陆成功
重复注册