乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      淺談MVVM框架

       huowufenghuang 2018-08-16

      MVC簡介

         
           MVC是Model-View-Controler的簡稱

             

            Model——即模型。模型一般都有很好的可復(fù)用性,統(tǒng)一管理一些我們需要使用的數(shù)據(jù)。

            View——就是存放視圖使用的。

            Controller——控制器它負(fù)責(zé)處理View和Model的事件。


      MVVM簡介

           
             MVC框架一目了然,也非常好理解,隨著App應(yīng)用功能的強(qiáng)大Controller的負(fù)擔(dān)越來越大因此在MVC的基礎(chǔ)上繁衍出了MVVM框架。

            ViewModel: 相比較于MVC新引入的視圖模型。是視圖顯示邏輯、驗(yàn)證邏輯、網(wǎng)絡(luò)請求等代碼存放的地方。

              現(xiàn)實(shí)開發(fā)中是找到一個(gè)合適的框架時(shí)使用,并不局限于哪一種,下面舉一個(gè)簡單的例子,在ViewModel里面處理業(yè)務(wù)邏輯,旨在講解MVVM框架,不用與工作,當(dāng)我們處理復(fù)雜的業(yè)務(wù)邏輯的時(shí)候可以優(yōu)先選擇MVVM框架。



      看圖簡單的邏輯,下面上代碼:

      User.h和User.m文件

      #import <Foundation/Foundation.h>

      @interface User : NSObject

      @property (nonatomic,copy) NSString *userName;

      @property (nonatomic,assign) NSInteger userId;

      @end


      #import "User.h"

      @implementation User

      - (id)init{

         self = [superinit];

         if (self) {

             self.userName  =@"";

             self.userId = 20;

          }

          returnself;

      }

      @end


      UserViewModel.h和UserViewModel.m 文件

      #import <Foundation/Foundation.h>

      @class User;

      @interface UserViewModel : NSObject

      @property (nonatomic,strong) User *user;

      @property (nonatomic,strong) NSString *userName;

      @end


      #import "UserViewModel.h"

      #import "User.h"

      @implementation UserViewModel

      - (id)init{

         self = [superinit];

         if (self) {

              //在這里處理業(yè)務(wù)邏輯

              _user = [[Useralloc]init];

              

             if (_user.userName.length > 0) {

                  _userName =_user.userName;

              }else {

                  _userName = [NSStringstringWithFormat:@"簡書%ld", (long)_user.userId];

              }

          }

          returnself;

      }


      @end


      ViewController.m文件


      #import "ViewController.h"

      #import "UserViewModel.h"

      @interface ViewController ()

      @property (nonatomic,strong) UILabel *userLabel;

      @property (nonatomic,strong) UserViewModel *userViewModel;

      @end


      @implementation ViewController


      - (void)viewDidLoad {

          [superviewDidLoad];

       

          _userLabel = [[UILabelalloc]initWithFrame:CGRectMake(10, 199, 200, 50)];

          _userLabel.backgroundColor = [UIColorredColor];

          _userViewModel = [[UserViewModelalloc]init];

          _userLabel.text =_userViewModel.userName;//顯示

          [self.viewaddSubview:_userLabel];

          

          // Do any additional setup after loading the view, typically from a nib.

      }



      這就是簡單的MVVM框架使用,工作中要靈活應(yīng)用,并不局限于哪一種。簡單的例子有助于我們理解MVVM框架

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多