「iOS」MVC模式

前言

在暑假的项目仿写中,随着项目难度的增加,代码量逐渐臃肿,这一点在ViewController视图中尤为明显。臃肿的代码量导致debug以及修改的时候十分苦难。
这时候MVC模式就显得十分重要


MVC模式

简介

MVC 的核心思想是将应用程序的逻辑(模型)与用户界面(视图和控制器)进行分离,以实现代码的可维护性和可扩展性。便于更好的组织代码,降低不同组件的耦合度,便于代码的复用和调试。

MVC,即Model—View—Controller,把软件系统分为三个部分:Model层,View层,Controller层,作为一种软件设计典范,目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化。通过对复杂度的简化,使程序结构更加直观。

  • M为Model层,负责储存并持有数据。提供数据接口给C层使用。
  • V为View层,负责接受用户操作并反馈视觉效果,即我们所说的页面。
  • C为Controller层,持有M层与V层,同时实现大部分的业务逻辑。

通俗来讲,就是把我们需要的数据在M层创建并初始化,把UI界面在V层创建并设置好,在C层使用M层和V层的数据和控件,加入业务逻辑。


使用

这里以一个用户登陆注册为例。
首先将相关文件分类
请添加图片描述
此处以登陆页面作为例子,注册页面与登陆页面同理。
Model层,创建并初始化数据。


//LandModel.h
#import <Foundation/Foundation.h>

@interface LandModel : NSObject
@property(nonatomic, strong)NSMutableArray *accoutArray;
@property(nonatomic, strong)NSMutableArray *passwordArray;
- (void)InitLandModel;
@end

//LandModel.m
#import "LandModel.h"

@implementation LandModel
- (void)InitLandModel {
    _passwordArray = [[NSMutableArray alloc] init];
    _accoutArray = [[NSMutableArray alloc] init];
}
@end

View层:创建登陆的UI界面。

//LandView.h
#import <UIKit/UIKit.h>

@interface LandView : UIView
@property(retain, nonatomic)UITextField *accountField;
@property(retain, nonatomic)UITextField *passwordField;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *registeBtn;
- (void)InitView;
@end

//LandView.m
#import "LandView.h"

@implementation LandView
- (void)InitView {
    
    //账号
    self.accountField = [[UITextField alloc]init];
    self.accountField.frame = CGRectMake(60, 350, 280, 40);
    self.accountField.placeholder = @"请输入账号";
    self.accountField.borderStyle = UITextBorderStyleRoundedRect;
    self.accountField.layer.cornerRadius = self.accountField.bounds.size.height / 2.0;
    self.accountField.layer.masksToBounds = YES;
    
    self.accountField.backgroundColor = [UIColor whiteColor];  // 设置背景颜色
    self.accountField.layer.borderColor = [UIColor blackColor].CGColor;  // 设置边框颜色
    self.accountField.layer.borderWidth = 1.0;  // 设置边框宽度
    [self.accountField becomeFirstResponder];
    [self addSubview:self.accountField];
    
    //密码
    self.passwordField = [[UITextField alloc]init];
    self.passwordField.frame = CGRectMake(60, 400, 280, 40);
    self.passwordField.placeholder = @"请输入密码";
    self.passwordField.borderStyle = UITextBorderStyleRoundedRect;
    
    self.passwordField.layer.cornerRadius = self.passwordField.bounds.size.height / 2.0;
    self.passwordField.layer.masksToBounds = YES;
    
    self.passwordField.backgroundColor = [UIColor whiteColor];  // 设置背景颜色
    self.passwordField.layer.borderColor = [UIColor blackColor].CGColor;  // 设置边框颜色
    self.passwordField.layer.borderWidth = 1.0;  // 设置边框宽度
    self.passwordField.secureTextEntry = YES;
    [self.passwordField becomeFirstResponder];
    [self addSubview:self.passwordField];
    
    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _loginBtn.frame = CGRectMake(80, 480, 80, 40);
    _loginBtn.layer.cornerRadius = _loginBtn.frame.size.height / 6.0;
    _loginBtn.layer.masksToBounds = YES;
    _loginBtn.layer.borderWidth = 2.0;
    _loginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    [_loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    _loginBtn.tintColor = [UIColor blackColor];
    _loginBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    _loginBtn.layer.borderColor = [UIColor blackColor].CGColor;
    [self addSubview:self.loginBtn];
    
    
    _registeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _registeBtn.frame = CGRectMake(233, 480, 80, 40);
    _registeBtn.layer.cornerRadius = _registeBtn.frame.size.height / 6.0;
    _registeBtn.layer.masksToBounds = YES;
    _registeBtn.layer.borderWidth = 2.0;
    _registeBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    [_registeBtn setTitle:@"注册" forState:UIControlStateNormal];
    _registeBtn.tintColor = [UIColor blackColor];
    _registeBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    _registeBtn.layer.borderColor = [UIColor blackColor].CGColor;
    [self addSubview:self.registeBtn];
    
}


@end

Controller层:持有并使用Model层和View层,添加登陆注册的业务逻辑,实现View和Model的交互。


#import <UIKit/UIKit.h>
#import "LandView.h"
#import "LandModel.h"
#import "RegistViewController.h"

@interface landViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic, strong)LandView *landView;
@property(nonatomic, strong)LandModel *landMode;
@property(retain, nonatomic) UIAlertController *alter;
@property(nonatomic, strong) RegistViewController *registerVC;
@end


#import "landViewController.h"

@interface landViewController ()

@end

@implementation landViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.landView = [[LandView alloc] initWithFrame:self.view.frame];
    [self.landView InitView];
    
    self.landMode = [[LandModel alloc] init];
    [self.landMode InitLandModel];
    [self.view addSubview:self.landView];
    
    [self.landView.loginBtn addTarget:self action:@selector(land_btn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.landView.registeBtn addTarget:self action:@selector(regester_btn:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)land_btn:(UIButton *) button
{
    int flag = 0;
    for (int i = 0; i < self.landMode.accoutArray.count; i++) {
        if([self.landMode.accoutArray[i] isEqual:self.landView.accountField.text]) {
            flag = i;
        }
    }
    
    
    if (self.landView.accountField.text.length != 0 ){
        
        if (self.landView.passwordField != nil && [self.landMode.passwordArray[flag] isEqual:self.landView.passwordField.text] ) {
            UIAlertController *_alertVier = [UIAlertController alertControllerWithTitle:@"登陆成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
            [_alertVier addAction:action01];
            [self presentViewController:_alertVier animated:YES completion:nil];
          
        } else {
            UIAlertController *_alertVier = [UIAlertController alertControllerWithTitle:@"登陆失败" message:@"账号与密码不同" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
            [_alertVier addAction:action01];
            [self presentViewController:_alertVier animated:YES completion:nil];

        }
    } else {
        UIAlertController *_alertVier = [UIAlertController alertControllerWithTitle:@"登陆失败" message:@"请输入账号" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
        [_alertVier addAction:action01];
        [self presentViewController:_alertVier animated:YES completion:nil];
    }
}

-(void)regester_btn:(UIButton *) button
{
    if (!_registerVC) {
        _registerVC = [[RegistViewController alloc] init];
        _registerVC.arrayDelegate = self;
        _registerVC.view.backgroundColor = [UIColor whiteColor];
    }
    
        
    [self.navigationController pushViewController:_registerVC animated:YES];
}

-(void)NSMutableArrayWithAccountDelegate:(NSMutableArray*) array_acount passWord:(NSMutableArray*)array_password
{
    self.landMode.accoutArray = array_acount;
    self.landMode.passwordArray = array_password;
}


@end


效果:
请添加图片描述


总结

在此只是对MVC模式进行简单的学习,往后使用MVC模式进行项目的仿写后,继续深入对MVC模式的学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值