【iOS】MVC模式

MVC模式

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

objectivec 复制代码
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface LandModel : NSObject
@property (nonatomic, copy) NSMutableArray* arr1;
@property (nonatomic, copy) NSMutableArray* arr2;
-(void) InitArray;
@end

NS_ASSUME_NONNULL_END
objectivec 复制代码
#import "LandModel.h"

@implementation LandModel
-(void) InitArray
{
    _arr1 = [[NSMutableArray alloc] init];
    _arr2 = [[NSMutableArray alloc] init];
}
@end

LandView

objectivec 复制代码
#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;
@end

NS_ASSUME_NONNULL_END
objectivec 复制代码
#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

objectivec 复制代码
#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
objectivec 复制代码
#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
相关推荐
Digitally4 小时前
如何在Mac上同步iPhone短信
macos·ios·iphone
2501_915106327 小时前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
2501_916008899 小时前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview
2501_915921439 小时前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
游戏开发爱好者811 小时前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview
qixingchao11 小时前
iOS SwiftUI 动画开发指南
ios·swiftui·swift
alengan11 小时前
ios支付
macos·ios·cocoa
00后程序员张11 小时前
混淆 iOS 类名与变量名的实战指南,多工具组合把混淆做成工程能力(混淆 iOS 类名变量名/IPA 成品混淆Ipa/Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview
MrZWCui12 小时前
iOS app语言切换
macos·ios·cocoa
晴天无痕14 小时前
iOS修改tabbar的背景图
macos·ios·cocoa