• iOS 架构模式-MVVM


    iOS 架构模式-MVVM


    MVVM

    Model-View-ViewModel
    MVVM 其实是MVC的进化版,他将业务逻辑从VC中解耦到ViewModel,实现VC的瘦身。

    做一个简单的登录判断:

    创建LoginViewModel(逻辑处理),LoginModel(只放数据),LoginViewController。 这里不用LoginView是为了能更好的把精力集中在用ViewModel解耦上。

    在LoginModel中加入方法

    //.h
    - (instancetype)initWithUserName:(NSString *)username password:(NSString *)password;
    
    @property (nonatomic,copy,readonly)NSString * username;
    @property (nonatomic,copy,readonly)NSString * password;
    
    //.m
    - (instancetype)initWithUserName:(NSString *)username password:(NSString *)password {
        if (self = [super init]) {
            _username = username;
            _password = password;
        }
        return self;
    }
    

    在LoginViewModel中加入方法

    //.h
    import "PersonModel.h" - (instancetype)initWithPerson:(PersonModel *)person; @property (nonatomic,assign,readonly)BOOL canLogin;


    //.m
    - (instancetype)initWithPerson:(PersonModel *)person {
        if (self = [super init]) {
         //在这做你绑定model后的处理
          _canLogin = [self valiCanLoginWithUserName:person.username password:person.password];
        }
        return self;
    }
    
    - (BOOL)valiCanLoginWithUserName:(NSString *)username password:(NSString *)password {
        if (username.length & password.length) {
            return YES;
        } else {
            return NO;
        }
    }
    

    然后VC(或者View)就可以直接这样获得判断后的结果

    PersonModel * person = [[PersonModel alloc]initWithUserName:@"10" password:@"10"];
    PersonViewModel * viewModel = [[PersonViewModel alloc]initWithPerson:person];
    NSLog(@"%d",viewModel.canLogin);
    

    简单的功能的时候没什么,当你处理复杂的逻辑判断的时候,MVVM会有巨大优势。

  • 相关阅读:
    Real-time 3D face tracking and reconstruction from 2D video
    Matlab Multiple View Geometry
    Multi-View 3D Reconstruction
    Scene Reconstruction
    OpenCV C++ Stereo Fisheye Calibration
    史上最全的Matlab资源电子书教程和视频下载合集
    CF-Based-Recommendation
    语种识别工具
    gdb调试
    C语言常见的函数调用
  • 原文地址:https://www.cnblogs.com/xubaoaichiyu/p/5452422.html
Copyright © 2020-2023  润新知