【iOS】MVC设计模式

ops/2024/10/18 7:52:58/

MVC

前言

如何设计一个程序的结构,这是一门专门的学问,叫做"架构模式"(architectural pattern),属于编程的方法论。MVC 模式就是架构模式的一种

它是Apple 官方推荐的 App 开发架构,也是一般开发者最先遇到、最经典的架构。

MVC各层

controller层

Controller / ViewController / VC(控制器)负责协调Model 和 View,处理大部分逻辑

它将数据从Model 层传送到View 层并展示出来,同时将View 层的交互传到Model 层以改变数据。**大部分的逻辑操作(点击Button就是一种逻辑)都应该交由VC完成。

model层

Model(模型)负责处理数据,以及处理部分的业务逻辑

通俗来说,就是你的程序是什么,就是你的程序将要实现的功能,或者是它所能干的事情。也就是微信消息列表里的人名字,信息内容,头像,是否屏蔽该人的消息等等数据,可以认为,Model 里面装满了这个程序的各种数据,它负责处理数据、以及处理部分的业务逻辑。

view层

V:View(视图)负责数据的展示和事件捕捉

通俗来说,在屏幕上你所看到的,比如一个UITableView,TableView 里面有UILabel,UIImageView,你在屏幕上看到的组件,都可以归类为View。

总结

总的来说,MVC模式就是将一个程序分为了三个部分,model负责处理数据,view负责处理视图,controller负责处理逻辑。但是model、view和controller之间并不是一一对应的关系。

⚠️Model 和 View 是相互独立的

胖model和瘦model

胖Model对应的是瘦的VC(Skinny Controller),在Model 中 对数据进行处理 ,让Controller可以直接使用经过处理后的数据。

瘦Model对应的是胖的VC(Fat Controller),Model中的数据 不进行任何处理或修改 ,原封不动的把服务器返回内容发送给Controller。

流程

用户点击view—>视图响应事件—>通过代理传递事件到controller—>发起网络请求更新model—>model处理完数据—>代理或通知给controller—>改变视图样式—>完成

优缺点

优点

通过controller控制全局,同时将view和model的变化分开,对于复杂的项目结构,有了明确的组织方式

缺点

大量逻辑代码放进controller,导致controller越来越臃肿,后期维护成本高

实例

LoginModel

@property(nonatomic, retain)NSMutableArray *accountArray;
@property(nonatomic, retain)NSMutableArray *passwordArray;
- (void)initLoginModel;
#import "LoginModel.h"@implementation LoginModel
- (void)initLoginModel {self.accountArray = [[NSMutableArray alloc] init];self.passwordArray = [[NSMutableArray alloc] init];[self.accountArray addObject:@"111"];[self.passwordArray addObject:@"111"];
}@end

LoginView

@interface LoginView : UIView@property(retain, nonatomic)UITextField *textfield1;
@property(retain, nonatomic)UITextField *textfield2;
@property(nonatomic, strong)UIButton *loginBtn;
@property(nonatomic, strong)UIButton *registerBtn;
- (void)initView;@end
#import "LoginView.h"@implementation LoginView- (void)initView {self.textfield1 = [[UITextField alloc] init];self.textfield1.frame = CGRectMake(20, 30, 280, 40);self.textfield1.placeholder = @"请输入账号";self.textfield1.borderStyle = UITextBorderStyleRoundedRect;[self.textfield1 becomeFirstResponder];[self addSubview:self.textfield1];self.textfield2 = [[UITextField alloc] init];self.textfield2.frame = CGRectMake(20, 80, 280, 40);self.textfield2.placeholder = @"请输入密码";self.textfield2.borderStyle = UITextBorderStyleRoundedRect;[self.textfield2 becomeFirstResponder];[self addSubview:self.textfield2];self.loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];self.frame = CGRectMake(120, 130, 80, 40);[_loginBtn setTitle:@"登录" forState:UIControlStateNormal];self.loginBtn.tintColor = [UIColor blackColor];self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:20];[self addSubview:self.loginBtn];self.registerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];self.registerBtn.frame = CGRectMake(120, 180, 80, 40);[self.registerBtn setTitle:@"注册" forState:UIControlStateNormal];[self addSubview:self.registerBtn];
}@end

LoginViewController

@interface LoginViewController : UIViewController@property (nonatomic, strong)LoginView *loginView;
@property (nonatomic, strong)LoginModel *loginModel;
@property (retain, nonatomic)UIAlertController *alert;@end
#import "LoginViewController.h"@interface LoginViewController ()@end@implementation LoginViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.loginModel = [[LoginModel alloc] init];[self.loginModel initLoginModel];self.loginView = [[LoginView alloc] initWithFrame:self.view.frame];[self.loginView initView];[self.view addSubview:self.loginView];[self.loginView.loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
}- (void)login {int flag = 0;for (int i = 0; i < _loginModel.accountArray.count; i ++) {if ([_loginModel.accountArray[i] isEqualToString:_loginView.textfield1.text] && [_loginModel.passwordArray[i] isEqualToString:_loginView.textfield2.text]) {flag = 1;break;}}if (flag == 1) {self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];[self.alert addAction:confirmAction];[self presentViewController:self.alert animated:YES completion:nil];} else {self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];[self.alert addAction:confirmAction];[self presentViewController:self.alert animated:YES completion:nil];}
}
@end


http://www.ppmy.cn/ops/109761.html

相关文章

SprinBoot+Vue校园数字化图书馆系统的设计与实现

目录 1 项目介绍2 项目截图3 核心代码3.1 Controller3.2 Service3.3 Dao3.4 application.yml3.5 SpringbootApplication3.5 Vue 4 数据库表设计5 文档参考6 计算机毕设选题推荐7 源码获取 1 项目介绍 博主个人介绍&#xff1a;CSDN认证博客专家&#xff0c;CSDN平台Java领域优质…

【第31章】Spring Cloud之Sentinel控制台推送规则到Nacos

文章目录 前言一、下载源码1. 下载源码 二、规则配置1. Nacos适配1.1 使用数据源1.2 复制官方案例1.3 动态规则配置中心 2. 前端路由配置3. 提示4. 编译和启动 三、测试1. 修改前2. 修改后 总结 前言 前面我们已经完成了通过nacos存储提供者流控配置文件&#xff0c;下面我们来…

干货分享|分享一款完全免费的PDF工具箱 PDF24

PDF 24介绍: PDF 24是一个全面的PDF工具包&#xff0c;旨在简化和优化PDF文件的处理和管理。它提供了广泛的功能和工具&#xff0c;包括创建、编辑、转换、合并、拆分、加密、解密、压缩、优化、签名、水印等。 主要特点&#xff1a;完全免费&#xff0c;轻量便捷&#xff0c;…

SpringBoot和Mybatis框架怎么防止SQL注入

在 Spring Boot 和 MyBatis 中&#xff0c;防止 SQL 注入的主要方法包括&#xff1a; 1.使用 MyBatis 的动态 SQL MyBatis 提供了安全构建 SQL 查询的方式&#xff0c;推荐使用动态 SQL 标签&#xff08;如 <if>、<choose>、<foreach> 等&#xff09;构建查…

HarmonyOS开发实战( Beta5.0)DevEco Device Tool开发环境搭建实践

通常在嵌入式开发中&#xff0c;很多开发者习惯于使用Windows进行代码的编辑&#xff0c;比如使用Windows的Visual Studio Code进行OpenHarmony代码的开发。但当前阶段&#xff0c;大部分的开发板源码还不支持在Windows环境下进行编译&#xff0c;如Hi3516、Hi3518系列开发板。…

[C++11#48][智能指针] RAII原则 | 智能指针的类型 | 模拟实现 | shared_ptr | 解决循环引用

目录 一.引入 1. 为什么需要智能指针&#xff1f; 2. 什么是内存泄漏&#xff1f; 内存泄漏分类 3.回忆 this 二. 原理 1. RAII 资源获取即初始化 2.像指针一样 三. 使用 1. 问题&#xff1a; string 的浅拷贝 2.解决 auto_ptr 自定义 auto_ptr unique_ptr - 独占…

【Dart 教程系列第 50 篇】在 Flutter 项目的国际化多语言中,如何根据翻译提供的多语言文档表格,快速生成不同语言的内容

这是【Dart 教程系列第 50 篇】&#xff0c;如果觉得有用的话&#xff0c;欢迎关注专栏。 博文当前所用 Flutter SDK&#xff1a;3.22.1、Dart SDK&#xff1a;3.4.1 文章目录 一&#xff1a;问题描述二&#xff1a;解决方案三&#xff1a;完整代码 一&#xff1a;问题描述 在…

Python精选200Tips:131-135

Put the final touches on something 131 python-docx - 创建和修改文档的库基本功能主要类和方法示例132 NetworkX - 网络分析和图论工具示例1: 小世界网络示例2:社交网络分析示例3:交通网络分析133 Statsmodels - 统计建模和计量经济学示例1:线性回归的残差分析示例2:年龄、性…