【iOS】MVC模式

2024-09-09 06:28
文章标签 模式 ios mvc

本文主要是介绍【iOS】MVC模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MVC模式

  • MVC模式
  • MVC模式demo

MVC模式

MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。

  • View:该层用于存放视图,该层中我们可以对页面及控件进行布局。
  • Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。
  • Controlller:该层充当一个CPU的功能,即该应用程序所有的工作都由Controller统一管调控,他负责处理View和Model的事件。

MVC模式降低了各个环节耦合性,优化了Controller中的代码量。

MVC模式中各个层级之间的关系图
在这里插入图片描述
在这个图中,我们可以看出,Model层和View层之间并不会进行直接通信,他们之间是依靠Controller进行通信的。
流程:
点击view,视图响应事件,而后通过代理传递事件到Controller,发起网络请求更新model,model处理完数据,代理或通知给Controller,改变视图样式,完成操作。

MVC模式demo

这里我写了一个登陆注册的demo来学习MVC模式的具体使用。
在这里插入图片描述
下面展示我登陆界面的代码:
LandModel

#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface LandModel : NSObject
@property (nonatomic, copy) NSMutableArray* arr1;
@property (nonatomic, copy) NSMutableArray* arr2;
-(void) InitArray;
@endNS_ASSUME_NONNULL_END
#import "LandModel.h"@implementation LandModel
-(void) InitArray
{_arr1 = [[NSMutableArray alloc] init];_arr2 = [[NSMutableArray alloc] init];
}
@end

LandView

#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface LandView : UIView
@property (nonatomic, retain) UITextField* textUser;
@property (nonatomic, retain) UITextField* textPassword;
@property (nonatomic, strong) UIButton* btn1;
@property (nonatomic, strong) UIButton* btn2;
-(void) InitButtonAndText;
@endNS_ASSUME_NONNULL_END
#import "LandView.h"@implementation LandView
- (void)InitButtonAndText
{self.textUser = [[UITextField alloc] initWithFrame:CGRectMake(50, 160, 300, 40)];self.textUser.borderStyle = UITextBorderStyleRoundedRect;    self.textUser.placeholder = @"请输入账号";[self addSubview:self.textUser];_textPassword = [[UITextField alloc] initWithFrame:CGRectMake(50, 230, 300, 40)];self.textPassword.borderStyle = UITextBorderStyleRoundedRect;self.textPassword.placeholder = @"请输入密码";[self addSubview:self.textPassword];_btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];_btn1.frame = CGRectMake(50, 350, 80, 50);[_btn1 setTitle:@"登陆" forState:UIControlStateNormal];_btn1.tintColor = [UIColor blackColor];[self addSubview:_btn1];_btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];_btn2.frame = CGRectMake(170, 350, 80, 50);[_btn2 setTitle:@"注册" forState:UIControlStateNormal];_btn2.tintColor = [UIColor blackColor];[self addSubview:_btn2];
}@end

ViewController

#import <UIKit/UIKit.h>
#import "LandModel.h"
#import "LandView.h"
#import "RegistViewController.h"@interface ViewController : UIViewController<SendDelegate>
@property (nonatomic, strong) LandView* landview;
@property (nonatomic, strong) LandModel* landmodel;
@property (nonatomic, strong) RegistViewController* regist;
@property (nonatomic, retain) UIAlertController* alert;@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_landview = [[LandView alloc] init];_landmodel = [[LandModel alloc] init];[self.view addSubview:_landview];[_landmodel InitArray];[_landview InitButtonAndText];[_landview.btn1 addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];[_landview.btn2 addTarget:self action:@selector(regist1) forControlEvents:UIControlEventTouchUpInside];
}-(void) login
{for(int i = 0; i < _landmodel.arr1.count; i++) {if([_landview.textUser.text isEqualToString:_landmodel.arr1[i]]&& [_landview.textPassword.text isEqualToString:_landmodel.arr2[i]]) {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];}}
}-(void) regist1
{if(!_regist) {_regist = [[RegistViewController alloc] init];}_regist.delegate = self;[self presentViewController:_regist animated:YES completion:nil];
}
- (void)send:(NSMutableArray *)arruser password:(NSMutableArray *)password {_landModel.arr1 = [NSMutableArray arrayWithArray:aarruser];_landModel.arr2 = [NSMutableArray arrayWithArray:password];
}@end

这篇关于【iOS】MVC模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1150412

相关文章

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

如何解决Spring MVC中响应乱码问题

《如何解决SpringMVC中响应乱码问题》:本文主要介绍如何解决SpringMVC中响应乱码问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC最新响应中乱码解决方式以前的解决办法这是比较通用的一种方法总结Spring MVC最新响应中乱码解

Spring MVC使用视图解析的问题解读

《SpringMVC使用视图解析的问题解读》:本文主要介绍SpringMVC使用视图解析的问题解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC使用视图解析1. 会使用视图解析的情况2. 不会使用视图解析的情况总结Spring MVC使用视图

基于@RequestParam注解之Spring MVC参数绑定的利器

《基于@RequestParam注解之SpringMVC参数绑定的利器》:本文主要介绍基于@RequestParam注解之SpringMVC参数绑定的利器,具有很好的参考价值,希望对大家有所帮助... 目录@RequestParam注解:Spring MVC参数绑定的利器什么是@RequestParam?@

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

Spring MVC跨域问题及解决

《SpringMVC跨域问题及解决》:本文主要介绍SpringMVC跨域问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录跨域问题不同的域同源策略解决方法1.CORS2.jsONP3.局部解决方案4.全局解决方法总结跨域问题不同的域协议、域名、端口

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

Spring MVC如何设置响应

《SpringMVC如何设置响应》本文介绍了如何在Spring框架中设置响应,并通过不同的注解返回静态页面、HTML片段和JSON数据,此外,还讲解了如何设置响应的状态码和Header... 目录1. 返回静态页面1.1 Spring 默认扫描路径1.2 @RestController2. 返回 html2