文章系列
《RACSignal 》
《RACDisposable》
《RACSubject窃判、RACReplaySubject》
《iOS RAC - 基本用法》
《iOS RAC - 定時器》
《iOS RAC - RACMulticastConnection》
《iOS RAC - RACCommand》
《iOS RAC - 核心方法bind》
《iOS RAC - 集合RACTuple燥爷、RACSequence》
《iOS RAC - rac_leftSelector》
《iOS RAC - 映射》
《iOS RAC - 過濾》
《iOS RAC - 登錄頁面,MVVM》
先布局UI
在storyboard中拖入兩個textField
和一個button
,在ViewController中引用谅辣,并且在storyboard中設(shè)置按鈕默認(rèn)不可以點(diǎn)擊修赞。
1、對用戶名和密碼做限制(用戶名長度不能少于1位桑阶,密碼必須是六位數(shù)及以上)
///監(jiān)聽文本框輸入狀態(tài)柏副,確定按鈕是否可以點(diǎn)擊
RAC(_loginBtn,enabled) = [RACSignal combineLatest:@[_accountTF.rac_textSignal,_passwordTF.rac_textSignal] reduce:^id _Nullable(NSString * account,NSString * password){
return @(account.length && (password.length > 5));
}];
2、監(jiān)聽按鈕點(diǎn)擊狀態(tài)
[[_loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"點(diǎn)擊了 點(diǎn)擊了");
}];
3蚣录、把按鈕的點(diǎn)擊事件割择,包裝成為一個command
在command中我們會模擬網(wǎng)絡(luò)請求,監(jiān)聽登錄成功的信號萎河,同時去監(jiān)聽command的執(zhí)行過程
RACCommand * btnPressCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
NSLog(@"組合參數(shù)荔泳,準(zhǔn)備發(fā)送登錄請求");
return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"開始請求");
NSLog(@"請求成功");
NSLog(@"處理數(shù)據(jù)");
[subscriber sendNext:@"請求完成,數(shù)據(jù)給你"];
return [RACDisposable disposableWithBlock:^{
NSLog(@"結(jié)束了");
}];
}];
}];
[btnPressCommand.executionSignals.switchToLatest subscribeNext:^(id _Nullable x) {
NSLog(@"登錄成功虐杯,跳轉(zhuǎn)頁面");
}];
[[btnPressCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
if ([x boolValue]) {
NSLog(@"正在執(zhí)行中……");
}else{
NSLog(@"執(zhí)行結(jié)束了");
}
}];
4玛歌、準(zhǔn)備工作都完成啦,現(xiàn)在在按鈕點(diǎn)擊的時候就執(zhí)行command
[[_loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"點(diǎn)擊了 點(diǎn)擊了");
[btnPressCommand execute:@{@"account":_accountTF.text,@"password":_passwordTF.text}];
}];
2017-10-28 11:04:07.912743+0800 RAC[467:49597] 點(diǎn)擊了 點(diǎn)擊了
2017-10-28 11:04:07.913775+0800 RAC[467:49597] 組合參數(shù)擎椰,準(zhǔn)備發(fā)送登錄請求 - {
account = jack;
password = 123456;
}
2017-10-28 11:04:07.918380+0800 RAC[467:49597] 正在執(zhí)行中……
2017-10-28 11:04:07.919022+0800 RAC[467:49597] 開始請求
2017-10-28 11:04:07.919115+0800 RAC[467:49597] 請求成功
2017-10-28 11:04:07.919196+0800 RAC[467:49597] 處理數(shù)據(jù)
2017-10-28 11:04:07.919688+0800 RAC[467:49597] 登錄成功支子,跳轉(zhuǎn)頁面
這里沒有執(zhí)行完成,原因是因為在commnad中返回的信號达舒,沒有調(diào)用sendCompleted
2017-10-28 11:06:21.270969+0800 RAC[471:50127] 結(jié)束了
2017-10-28 11:06:21.271169+0800 RAC[471:50127] 執(zhí)行結(jié)束了
MVVM
在剛才的代碼中我們實現(xiàn)了功能值朋,但是全部都寫在了VC中叹侄,這樣子顯然是不夠好的,所以我們一般都會采取MVC對VC進(jìn)行瘦身吞歼,在RAC中更多的時候是使用MVVM圈膏。
所以接下來就使用MVVM來把VC瘦身吧
1塔猾、創(chuàng)建VM
創(chuàng)建一個VMLoginViewModel
篙骡,導(dǎo)入到VC中,并且懶加載他
@property (nonatomic, strong) LoginViewModel *loginVM;
@implementation ViewController
- (LoginViewModel *)loginVM{
if (!_loginVM){
_loginVM = [[LoginViewModel alloc] init];
}
return _loginVM;
}
2丈甸、開始抽離
- 2.1抽離文本框邏輯(這個部分VC并不關(guān)心糯俗,所以需要抽離出來)
文本框有兩個,一個是用戶名睦擂,一個是密碼得湘,兩個文本框的值我們需要知道并保存,所以需要添加兩個屬性account
顿仇、password
淘正、
@property (nonatomic, strong) NSString *account;
@property (nonatomic, strong) NSString *password;
然后在VC中賦值
RAC(self.loginVM,account) = _accountTF.rac_textSignal;
RAC(self.loginVM,password) = _passwordTF.rac_textSignal;
現(xiàn)在拿到了值,那么我要把結(jié)果告訴給VC臼闻,所以我們還需要創(chuàng)建一個Signal
@property (nonatomic, strong) RACSignal *btnEnableSignal;
信號肯定要初始化對吧
- (instancetype)init
{
self = [super init];
if (self) {
[self setUp];
}
return self;
}
- (void)setUp{
//RACObserve可以把KVO轉(zhuǎn)化為信號
_btnEnableSignal = [RACSignal combineLatest:@[RACObserve(self,account),RACObserve(self, password)] reduce:^id _Nullable(NSString * account,NSString * password){
return @(account.length && (password.length > 5));
}];
}
現(xiàn)在只需要在VC中訂閱我們這個信號就好了
RAC(_loginBtn,enabled) = self.loginVM.btnEnableSignal;
2.2 抽離command
(VC也不關(guān)心你怎么處理數(shù)據(jù)鸿吆,怎么去請求,VC只需要知道結(jié)果就夠了)
創(chuàng)建一個command
@property (nonatomic, strong) RACCommand *loginCommand;
然后就是把剛才寫好的代碼賦值到VM的setUp
方法當(dāng)中
- (void)setUp{
//RACObserve可以把KVO轉(zhuǎn)化為信號
_btnEnableSignal = [RACSignal combineLatest:@[RACObserve(self,account),RACObserve(self, password)] reduce:^id _Nullable(NSString * account,NSString * password){
return @(account.length && (password.length > 5));
}];
_loginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
NSLog(@"組合參數(shù)述呐,準(zhǔn)備發(fā)送登錄請求 - %@",input);
return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"開始請求");
NSLog(@"請求成功");
NSLog(@"處理數(shù)據(jù)");
[subscriber sendNext:@"請求完成惩淳,數(shù)據(jù)給你"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"結(jié)束了");
}];
}];
}];
[[_loginCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
if ([x boolValue]) {
NSLog(@"正在執(zhí)行中……");
}else{
NSLog(@"執(zhí)行結(jié)束了");
}
}];
}
最后把按鈕按下的時候執(zhí)行的command
改成為VM里面的command
就可以了。
[[_loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"點(diǎn)擊了 點(diǎn)擊了");
[self.loginVM.loginCommand execute:@{@"account":_accountTF.text,@"password":_passwordTF.text}];
}];
最終使用MVVM之后呢乓搬,在VC的代碼就是這樣子的
- (void)viewDidLoad {
[super viewDidLoad];
RAC(self.loginVM,account) = _accountTF.rac_textSignal;
RAC(self.loginVM,password) = _passwordTF.rac_textSignal;
RAC(_loginBtn,enabled) = self.loginVM.btnEnableSignal;
[self.loginVM.loginCommand.executionSignals.switchToLatest subscribeNext:^(id _Nullable x) {
NSLog(@"登錄成功思犁,跳轉(zhuǎn)頁面");
}];
[[_loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"點(diǎn)擊了 點(diǎn)擊了");
[self.loginVM.loginCommand execute:@{@"account":_accountTF.text,@"password":_passwordTF.text}];
}];
}
相比較于之前什么都在VC中處理,確實給VC瘦身不少