什么是MVC

MVC是Model-View-Controller的简写,它表示的是一种常见的客户端软件开发框架。可见他是由三个部分组成的。
下面解释一下这三个部分:
Model:即为模型,用来存储业务数据, 具有很好的复用性,我们也可以在这个模型中完成对数据库中数据的增删查改。
View:即为视图,用来显示业务需要的UI控件,以实现人机交互。
Controller:控制器,这里可以将其理解为一个CPU,负责处理View和Mode的事件,应用程序中所有的工作都由控制器统一调控。比如用户的操作就是由控制器实现的。

MVC模式能够完成各司其职的任务模式,由于降低了各个环节的耦合性,大大优化Controller的代码量,当程序调试时,如果某一个功能没有按照既定的模式工作,可以很方便的定位到到底是Controller还是View还是Model出了问题,而且还利于程序的可复用性

MVC的原理

上面大概介绍了MVC的组成,下面具体介绍一下MVC三个组成之间的逻辑关系,下图生动形象的描述了这个关系:

1、 Controller和View之间可以通信,Controllor通过outlet(输出口)控制View,View可以通过target-action、delegate或者data source(想想UITableVeiwDatasource)来和Controller通信;

2、 Controller在接收到View传过来的交互事件(View就是完成让人和程序的交互的呀,比如按B1按钮)之后,经过一些判断和处理,把需要Model处理的事件递交给Model处理(比如刚才的例子中的保存到数据库),Controller对Model使用的是API;

3、 Model在处理完数据之后,如果有需要,会通过Notification或者KVO的方式告知Controller,事件已经处理完,Controller再经过判断和处理之后,考虑下一步要怎么办(是默默无闻的在后台操作,还是需要更新View,这得看Controller的“脸色”行事)。这里的无线天线很有意思,Model只负责发送通知,具体谁接收这个通知并处理它,Model并不关心,这一点非常重要,是理解Notification模式的关键。

4、 Model和View之间不直接通信!

上面部分摘自:
实际案例讲解iOS——MVC设计模式

如何使用MVC设计模式

下面举了一个登录注册的小Demo。
首先我们得将项目的文件根据MVC创建,如下所示:

一般来说创建几个视图就得创建几套MVC文件,
这里的LandModel继承于NSObject,
LandView继承于UIView。
LandController继承于VIewController。
下面将介绍在三个文件中都应该写什么。

Model文件

在上面已经介绍过,Model主要用来存储业务数据,这里用来存储登录时的初始密码。

//  LandModel.h
//  MVC Demo
//
//  Created by 差不多先生 on 2021/9/7.
//#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface LandModel : NSObject
@property (nonatomic, strong) NSString* idString;
@property (nonatomic, strong) NSString* passwordString;
- (void) landModelInit;
@endNS_ASSUME_NONNULL_END
//  LandModel.m
//  MVC Demo
//
//  Created by 差不多先生 on 2021/9/7.
//#import "LandModel.h"@implementation LandModel
- (void) landModelInit {_idString = [[NSString alloc] init];_passwordString = [[NSString alloc] init];_idString = @"123";_passwordString = @"123";
}
@end

View文件

上面介绍过,View文件是控制UI控件的文件。

//  Created by 差不多先生 on 2021/9/7.
//#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface LandView : UIView
@property (nonatomic, strong) UITextField* idTextField;
@property (nonatomic, strong) UITextField* passwordTextField;
@property (nonatomic, strong) UIButton* landButton;
@property (nonatomic, strong) UIButton* registerButton;@endNS_ASSUME_NONNULL_END
//  LandView.m
//  MVC Demo
//
//  Created by 差不多先生 on 2021/9/7.
//#import "LandView.h"@implementation LandView
- (id)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];self.backgroundColor = [UIColor orangeColor];_idTextField = [[UITextField alloc] init];_idTextField.frame = CGRectMake(80, 150, 250, 40);_idTextField.backgroundColor = [UIColor whiteColor];[self addSubview:_idTextField];_idTextField.placeholder = @"please input your id";_passwordTextField = [[UITextField alloc] init];_passwordTextField.frame = CGRectMake(80, 250, 250, 40);_passwordTextField.placeholder = @"please input your password";_passwordTextField.backgroundColor = [UIColor whiteColor];_passwordTextField.secureTextEntry = YES;[self addSubview:_passwordTextField];_landButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];[_landButton setTitle:@"Land" forState:UIControlStateNormal];_landButton.frame = CGRectMake(120, 350, 70, 30);_registerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];[_registerButton setTitle:@"register" forState:UIControlStateNormal];_registerButton.frame = CGRectMake(210, 350, 70, 30);[self addSubview:_landButton];[self addSubview:_registerButton];return self;
}

注意这里我们重写了initWithFrame方法,这是因为我们创建了一个UIView的子类,这时候将调用这个方法来实例化对象。
特别注意,如果在子类中重载initWithFrame方法,必须先调用父类的initWithFrame方法。在对自定义的UIView子类进行初始化操作。)
我们重写完方法后,只需在Controller文件中直接创建UIView对象即可。

Controller文件

控制器主要控制各种事件操作,例如我们点击登录时进行的逻辑操作,是在Controller文件中书写的,同样一些代理方法也可以在此文件中实现。

//
//  LandController.m
//  MVC Demo
//
//  Created by 差不多先生 on 2021/9/7.
//#import "LandController.h"
#import "LandView.h"
#import "LandModel.h"
#import "RegisterViewController.h"
@interface LandController ()
@property (nonatomic, strong) LandView* landView;
@property (nonatomic, strong) LandModel* landModel;
@end@implementation LandController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view._landView = [[LandView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];[self.view addSubview:_landView];_landModel = [[LandModel alloc] init];[_landModel landModelInit];[_landView.landButton addTarget:self action:@selector(SignButtonPress) forControlEvents:UIControlEventTouchUpInside];[_landView.registerButton addTarget:self action:@selector(registerPress) forControlEvents:UIControlEventTouchUpInside];
}
- (void) SignButtonPress {if ([_landView.idTextField.text isEqualToString:@""] || [_landView.passwordTextField.text isEqualToString:@""]) {UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请完整输入" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认"  style:UIAlertActionStyleDefault handler:nil];[alertController addAction:sureAction];[self presentViewController:alertController animated:YES completion:nil];} else if ([_landView.idTextField.text isEqual:_landModel.idString] && [_landView.passwordTextField.text isEqual:_landModel.passwordString]) {UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认"  style:UIAlertActionStyleDefault handler:nil];[alertController addAction:sureAction];[self presentViewController:alertController animated:YES completion:nil];} else {UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码错误" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认"  style:UIAlertActionStyleDefault handler:nil];[alertController addAction:sureAction];[self presentViewController:alertController animated:YES completion:nil];}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {[_landView.idTextField resignFirstResponder];[_landView.passwordTextField resignFirstResponder];}
- (void)registerPress {RegisterViewController* viewController = [[RegisterViewController alloc] init];viewController.modalPresentationStyle = UIModalPresentationFullScreen;viewController.delegate = self;[self presentViewController:viewController animated:YES completion:nil];
}
- (void)returnId:(NSString *)ida andPass:(NSString *)password {_landModel.idString = ida;_landModel.passwordString = password;
}
/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end

iOS——MVC设计模式相关推荐

  1. iOS MVC设计模式

    说起设计模式,感觉自己把握不了笔头,所以单拿出iOS开发中的几种常用设计模式谈一下. 单例模式(Singleton) 概念:整个应用或系统只能有该类的一个实例 在iOS开发我们经常碰到只需要某类一个实 ...

  2. iOS中MVC设计模式

    在组织大型项目的代码文件时,我们常用MVC的思想.MVC的概念讲起来非常简单,就和对象(object)一样.但是理解和应用起来却非常困难.今天我们就简单总结一下MVC设计理念. MVC(Model V ...

  3. 正确的理解iOS MVC

    MVC 刚接触iOS开发时,就知道Apple公司为我们提供了MVC模式,并且SDK设计也告诉我们应该搭配MVC使用.所以每一个人都应该掌握这种模式,那么,什么是MVC了? 被误解的MVC 根据MVC关 ...

  4. Flutter开发之MVC设计模式:新建文件与导入文件(八)

    在前面的例子中,所有的代码和路由都是在main.dart 下写的.我们知道不管后台还是前端,不管是Android 还是iOS开发,都是基于MVC设计模式开发的.那么flutter是怎么使用MVC设计模 ...

  5. MVC设计模式深入理解

    MVC要实现的目标是将软件用户界面和业务逻辑分离以使代码可扩展性.可复用性.可维护性.灵活性加强. MVC是三个单词的缩写:Model, View, Controller. View层是界面,Mode ...

  6. Xamarin简介与Xamarin支持MVC设计模式

    Create Native iOS, Android, Mac and Windows apps in C#. 官方网站:http://xamarin.com/ 使用武器 Run a C# app, ...

  7. mvc设计模式现在过时了吗_尚学堂115——设计模式、源码分析以及SpringData

    设计模式 什么是设计模式?你是否在你的代码里面使用过任何设计模式? 设计模式是在软件设计中常见问题的通用.可反复使用.多数人知晓的一种解决方案或模板:这些解决方案是在相当长的一段时间内由众多软件开发人 ...

  8. 第80节:Java中的MVC设计模式

    第80节:Java中的MVC设计模式 前言 了解java中的mvc模式.复习以及回顾! 事务,设置自动连接提交关闭. setAutoCommit(false); conn.commit(); conn ...

  9. IT兄弟连 JavaWeb教程 MVC设计模式

    MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它强制性地把应用程序的数据展示.数据处理和流程控制分开.MVC把应用程序分成3个核心模块:模型.视 ...

最新文章

  1. 2022-2028年中国肉制品行业市场调查研究及前瞻分析报告
  2. 这些MySQL配置“修改条令”,你有必要熟识默记!
  3. AMD全球产业链上的“中国环”会有多给力?
  4. 亲爱的,我是一条Linux运维技术学习路径呀。
  5. typedef 数组使用详解
  6. 作者:项连城(1992-),女,中国科学院自动化研究所硕士生
  7. tcpip网络编程 尹圣雨源码_网络编程——学习笔记
  8. 介绍计算机的英文文章,计算机方面的英语资料,介绍一些计算机的英语短文,有兴趣的可以看...
  9. 完全卸载go语言编译器
  10. 性能VS功能,同为测试又有哪些不一样?
  11. Effective C++ 之 Item 5:了解C++默默编写并调用哪些函数
  12. 小公司如何部署实施Linux集群网站【转载】
  13. flask-login
  14. rpcbind服务没法开启问题
  15. 4、RH850 F1 定时器OSTM功能和配置
  16. SAP中检验计划创建晚于检验批导致无法质检的问题分析处理
  17. 如何将IP地址批量改变为城域网的IP地址
  18. STM32H7B0 HAL库中关于DMA的注意事项以及DCMI调试遇到的问题及解决方法
  19. 用阿里云香港云服务器时需要注意的方面
  20. 【Plant Cell Physiol】R2R3-MYB调节因子FhPAP1在香雪兰花青素生物合成中的作用

热门文章

  1. 在Ubuntu下编译运行C语言程序
  2. Linux(Ubuntu)下C语言编译与调试
  3. 一些标签属性--aria-label--rel属性
  4. MATLAB应用:第二章-基本使用方法
  5. maya批量操作mel_maya为热键指定 MEL 脚本,MAYA
  6. 2021-12-08:扑克牌中的红桃J和梅花Q找不到了,为了利用剩下的牌做游戏,小明设计了新的游戏规则: 1) A,2,3,4....10,J,Q,K分别对应1到13这些数字,大小王对应0; 2) 游
  7. 无线通信网知识点笔记
  8. day18-面向对象程序进阶和继承(10.13)
  9. 随着计算机技术的快速发展,随着计算机技术的发展,计算机的应用范围也越来越广...
  10. matlab的损失函数mse,MSELoss损失函数