【iOS】MVC


序文

MVC パターンの目的は、動的プログラミング設計を実装し、その後のプログラムの変更と拡張を簡素化し、プログラムの特定の部分を再利用できるようにすることです。さらに、このモードは複雑さを単純化することでプログラム構造をより直感的にします。

1. MVC の各層の責任

1.1. コントローラー層

  • ビューを生成してからビューを組み立てる
  • View イベントに応答し、ビューのプロキシとして機能します。
  • ビューのライフサイクルの処理
  • インターフェイス間のジャンプを処理する
  • モデルのデータ取得インターフェイスを呼び出し、返されたデータを取得して処理し、表示するためにビューにレンダリングします。

1.2. モデル層

  • ビジネスロジックのカプセル化
  • コントローラーが使用するデータインターフェースを提供する
  • データの永続的な保存と読み取り
  • データをデータモデルとして保存する

1.3. ビューレイヤー

  • インターフェース要素の構築、アニメーション効果、データ表示
  • ユーザーのアクションを受け入れ、視覚的なフィードバックを提供します

ここに画像の説明を挿入します

2. まとめ

ユーザーは、ビューをクリックします –> ビューがイベントに応答します –> プロキシ経由でイベントをコントローラーに転送します –> モデルを更新するネットワーク要求を開始します –> モデルがデータを処理します –> コントローラーに委任または通知します –> 変更ビュー スタイル –> 完了

3. メリットとデメリット

3.1. 利点

コントローラーを通じて全体の状況を制御し、ビューとモデルの変更を分離することで、複雑で混沌としたプロジェクト構造を明確に整理することができます。

3.2. 欠点

ビジネス ロジックが増加すると、コントローラーに大量のロジック コードが組み込まれるため、コントローラーはますます肥大化し、将来的には高額な保守コストが発生します。

4. コード例

MVC フレームワークを実装するために、小さなログインと登録のデモを使用します。まず、ファイルの命名を見てください:
ここに画像の説明を挿入します
作成者のログインと登録がそれぞれ MVC を実装していることがわかります。ここでは、ログインに基づいた MVC の説明を示します。


モデル:

//
//  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

ビュー:

//
//  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];
    
}

コントローラ:

//
//  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

アニメーションを実行します。
ここに画像の説明を挿入します


おすすめ

転載: blog.csdn.net/weixin_72437555/article/details/132794591