【iOS】MVC


Preface

The purpose of the MVC pattern is to implement a dynamic programming design, simplify subsequent modifications and expansions of the program, and make it possible to reuse certain parts of the program. In addition, this mode makes the program structure more intuitive by simplifying the complexity.

1. Responsibilities of each layer of MVC

1.1. Controller layer

  • Generate the view and then assemble the view
  • Respond to View events and serve as a proxy for the view
  • Handling the life cycle of the view
  • Handle jumps between interfaces
  • Call the data acquisition interface of the model, get the returned data, process it, and render it to the view for display.

1.2. Model layer

  • Business logic encapsulation
  • Provide data interface for controller to use
  • Data persistence storage and reading
  • Store data as a data model

1.3. view layer

  • Interface element construction, animation effects, data display
  • Accept user actions and provide visual feedback

Insert image description here

2. Summary

The user clicks the View–>The view responds to the event–>Transfers the event to the Controller through the proxy–>Initiates a network request to update the Model–>Model processes the data–>Delegates or notifies the Controller–>Changes the view style–>Complete

3. Advantages and Disadvantages

3.1. Advantages

Control the overall situation through the Controller, and separate the changes in the view and the Model. There is a clear way to organize the complex and chaotic project structure.

3.2. Disadvantages

As business logic increases, a large amount of logic code is put into the Controller, causing the Controller to become more and more bloated and causing high maintenance costs in the future.

4. Code examples

We use a small login and registration demo to implement our MVC framework. First, take a look at our file naming:
Insert image description here
You can see that the author's login and registration have implemented MVC respectively. Here is an explanation of MVC based on login.


Model:

//
//  LandModel.h
//  MVC学习
//
//  Created by 夏楠 on 2023/9/9.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

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

NS_ASSUME_NONNULL_END
//
//  LandModel.m
//  MVC学习
//
//  Created by 夏楠 on 2023/9/9.
//

#import "LandModel.h"

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

View:

//
//  LandView.h
//  MVC学习
//
//  Created by 夏楠 on 2023/9/9.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

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

NS_ASSUME_NONNULL_END

//
//  LandView.m
//  MVC学习
//
//  Created by 夏楠 on 2023/9/9.
//

#import "LandView.h"

@implementation LandView

- (void)InitView {
    
    
    
    //账号
    self.textField1 = [[UITextField alloc]init];
    self.textField1.frame = CGRectMake(60, 350, 280, 40);
    self.textField1.placeholder = @"请输入账号";
    self.textField1.borderStyle = UITextBorderStyleRoundedRect;
    // 设置文本框的圆角
    self.textField1.layer.cornerRadius = self.textField1.bounds.size.height / 2.0;
    self.textField1.layer.masksToBounds = YES;
    self.textField1.backgroundColor = [UIColor whiteColor];  // 设置背景颜色
    self.textField1.layer.borderColor = [UIColor blackColor].CGColor;  // 设置边框颜色
    self.textField1.layer.borderWidth = 1.0;  // 设置边框宽度
    [self.textField1 becomeFirstResponder];
    [self addSubview:self.textField1];
    
    //self.textField1.delegate = self;
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    //用在Controller层
    
    //密码
    self.textField2 = [[UITextField alloc]init];
    self.textField2.frame = CGRectMake(60, 400, 280, 40);
    self.textField2.placeholder = @"请输入密码";
    self.textField2.borderStyle = UITextBorderStyleRoundedRect;
    // 设置文本框的圆角
    self.textField2.layer.cornerRadius = self.textField2.bounds.size.height / 2.0;
    self.textField2.layer.masksToBounds = YES;
    self.textField2.backgroundColor = [UIColor whiteColor];  // 设置背景颜色
    self.textField2.layer.borderColor = [UIColor blackColor].CGColor;  // 设置边框颜色
    self.textField2.layer.borderWidth = 1.0;  // 设置边框宽度
    self.textField2.secureTextEntry = YES;
    [self.textField2 becomeFirstResponder];
    [self addSubview:self.textField2];

//    self.textField2.delegate = self;
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    //用在Controller层

    _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];
    
//    [_loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    //用在controller层
    
    _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];

    //添加注册时间
    
//    [_registeBtn addTarget:self action:@selector(registe) forControlEvents:UIControlEventTouchUpInside];
    
}

Controller:

//
//  ViewController.h
//  MVC学习
//
//  Created by 夏楠 on 2023/9/9.
//

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

@interface ViewController : UIViewController<UITextFieldDelegate, ConfirmDelegate>
@property (nonatomic, strong)LandView *landView;
@property (nonatomic, strong)LandModel *landModel;
@property (retain, nonatomic)UIAlertController *alert;
@property (nonatomic, strong)RegistViewController *rVC;

@end


//
//  ViewController.m
//  MVC学习
//
//  Created by 夏楠 on 2023/9/9.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    _landModel = [[LandModel alloc] init];
    [_landModel InitLandModel];
    
    _landView = [[LandView alloc] initWithFrame:self.view.frame];
    [_landView InitView];
    [self.view addSubview:_landView];
    
    [_landView.loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [_landView.registeBtn addTarget:self action:@selector(registe) forControlEvents:UIControlEventTouchUpInside];
}

登陆函数
- (void)login {
    
    

    int boo1 = 0;
    for (int i = 0; i < _landModel.accoutArray.count; i ++) {
    
    
        if ([_landModel.accoutArray[i] isEqualToString:_landView.textField1.text] && [_landModel.passwordArray[i] isEqualToString:_landView.textField2.text]) {
    
    
            boo1 = 1;
            break;
        }
    }

        if (boo1 == 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];
        }
    }

- (void)registe {
    
    
    if (!_rVC)
    _rVC = [[RegistViewController alloc] init];
    _rVC.delegate = self;
    NSLog(@"%@, %@", _landModel.accoutArray, _landModel.passwordArray);
    [self presentViewController:_rVC animated:YES completion:nil];
}

- (void)confirm:(NSMutableArray *)account password:(NSMutableArray *)password {
    
    
    _landModel.accoutArray = [NSMutableArray arrayWithArray:account];
    _landModel.passwordArray = [NSMutableArray arrayWithArray:password];
}


@end

Run animation:
Insert image description here


Guess you like

Origin blog.csdn.net/weixin_72437555/article/details/132794591
ios