MVC简介
Model——即模型。模型一般都有很好的可复用性,统一管理一些我们需要使用的数据。
View——就是存放视图使用的。
Controller——控制器它负责处理View和Model的事件。
MVVM简介
#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
#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) {
//在这里处理业务逻辑
_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.
}