被錯誤理解和使用的MVC

現在的工作中在使用MVVM(學習中),今天突然意識到之前對MVC理解錯了。

現在的理解

應用就是與用戶操作,處理一系列的數據后再呈現給用戶。
model是處理這些數據用的,比如請求、映射、過濾、排序……
view是用來顯示數據、與用戶交互的。
controller是用來協(xié)調V與M的。

比如,舉個例子,下面是部分代碼。具體的請移步這里

Model.h

#import <Foundation/Foundation.h>

@interface Model : NSObject

@property (nonatomic, strong) NSArray *personArray;

- (void)fetchPersonArray;

@end

Model.m

#import "Model.h"
#import "Person.h"

@implementation Model

- (void)fetchPersonArray {
    // 請求網絡
    // 或者
    // 訪問數據庫

    Person *person1 = [Person personWithName:@"aaaaa" age:1];
    Person *person2 = [Person personWithName:@"bbbbb" age:2];
    Person *person3 = [Person personWithName:@"ccccc" age:3];
    Person *person4 = [Person personWithName:@"ddddd" age:4];

    self.personArray = @[person1, person2, person3, person4];
}

@end

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;

+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age;

@end

ViewController.m

#import "ViewController.h"
#import "Model.h"
#import "Person.h"
#import "View.h"

@interface ViewController ()

@property (nonatomic, strong) Model *model;
@property (nonatomic, strong) View *customView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.model = [[Model alloc] init];
    [self.model addObserver:self forKeyPath:@"personArray" options:NSKeyValueObservingOptionNew context:nil];

    self.customView = [[View alloc] init];
    self.customView.frame = self.view.bounds;
    [self.view addSubview:self.customView];

    [self.model fetchPersonArray];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"personArray"]) {
        [self updateUI];
    }
}

- (void)updateUI {
    self.customView.personName1.text = [self.model.personArray[0] name];
    self.customView.personName2.text = [self.model.personArray[1] name];
    self.customView.personName3.text = [self.model.personArray[2] name];
    self.customView.personName4.text = [self.model.personArray[3] name];
}

@end

當然以上的理解還不敢說正確,但是可以肯定的是我之前是錯了。
感覺挺慚愧的,錯了這么久,謹記?。。?/p>

之前的理解(錯誤)

model是數據模型,比如上面中Person的角色
view是用來顯示數據、與用戶交互的。
controller是用來處理邏輯,協(xié)調V與M的。

將上面的例子用錯誤的方式實現如下,具體代碼請移步這里

Model.h

#import <Foundation/Foundation.h>

@interface Model : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;

+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age;

@end

ViewController.m

#import "ViewController.h"
#import "Model.h"
#import "View.h"

@interface ViewController ()

@property (nonatomic, strong) NSArray *personArray;
@property (nonatomic, strong) View *customView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.customView = [[View alloc] init];
    self.customView.frame = self.view.bounds;
    [self.view addSubview:self.customView];

    __weak ViewController *weakSelf = self;
    [self fetchPersonArray:^{
        [weakSelf updateUI];
    }];
}

- (void)fetchPersonArray:(void (^)(void))completion {
    Model *person1 = [Model personWithName:@"aaaaa" age:1];
    Model *person2 = [Model personWithName:@"bbbbb" age:2];
    Model *person3 = [Model personWithName:@"ccccc" age:3];
    Model *person4 = [Model personWithName:@"ddddd" age:4];

    self.personArray = @[person1, person2, person3, person4];

    if (completion) {
        completion();
    }
}

- (void)updateUI {
    self.customView.personName1.text = [self.personArray[0] name];
    self.customView.personName2.text = [self.personArray[1] name];
    self.customView.personName3.text = [self.personArray[2] name];
    self.customView.personName4.text = [self.personArray[3] name];
}

@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容