1.Cocoapods导入ReactiveCocoa:
use_frameworks! target 'RACDemo' do pod 'ReactiveObjC', '~> 2.1.0' end
不懂得可以看这个链接: http://www.tuicool.com/articles/Qju6fme
2.输出文本内容操作:
// // ViewController.m // RACDemo // // Created by 思 彭 on 17/1/4. // Copyright © 2017年 思 彭. All rights reserved. // #import "ViewController.h" #import <ReactiveObjC/ReactiveObjC.h> @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *userNameLabel; @property (weak, nonatomic) IBOutlet UILabel *passwordLabel; @property (weak, nonatomic) IBOutlet UITextField *userNameTF; @property (weak, nonatomic) IBOutlet UITextField *passwordTF; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 1.每次改变文本框中的文字,block中的代码都会执行。没有target-action,没有delegate,只有signal和block [self.userNameTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) { NSLog(@"text = %@",x); }]; // 2.只关心超过3个字符长度的用户名 [[self.userNameTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) { NSString *text = value; return text.length > 3; }] subscribeNext:^(NSString * _Nullable x) { NSLog(@"---%@",x); }]; // 3.上面步骤分解如下: RACSignal *userNameSourceSignal = self.userNameTF.rac_textSignal; RACSignal *filteredUsername = [userNameSourceSignal filter:^BOOL(id _Nullable value) { NSString *text = value; return text.length > 3; }]; [filteredUsername subscribeNext:^(id _Nullable x) { NSLog(@"---%@",x); }]; } @end
3.输出文本长度操作:
// 1.输出文本长度 > 的长度: // map操作之后的步骤收到的都是NSNumber实例。你可以使用map操作来把接收的数据转换成想要的类型,只要它是个对象。 [[[self.userNameTF.rac_textSignal map:^id _Nullable(NSString * _Nullable value) { return @(value.length); }] filter:^BOOL(id _Nullable value) { return [value integerValue] > 3; }] subscribeNext:^(id _Nullable x) { NSLog(@"length = %@",x); }];
4.创建一些信号,来表示用户名和密码输入框中的输入内容是否有效,设置背景颜色:
// 2.创建一些信号,来表示用户名和密码输入框中的输入内容是否有效 RACSignal *validUserNameSignal = [self.userNameTF.rac_textSignal map:^id _Nullable(NSString * _Nullable value) { return @([self isValidUsername:value]); // 输出是一个用NSNumber封装的布尔值 }]; RACSignal *validPasseordSifnal = [self.passwordTF.rac_textSignal map:^id _Nullable(NSString * _Nullable value) { return @([self isValidPassword:value]); }]; // 3.转换这些信号,从而能为输入框设置不同的背景颜色 [[validUserNameSignal map:^id _Nullable(id _Nullable value) { return [value boolValue] ? [UIColor clearColor] : [UIColor yellowColor]; }] subscribeNext:^(id _Nullable x) { self.passwordTF.backgroundColor = x; }]; [[validPasseordSifnal map:^id _Nullable(id _Nullable value) { return [value boolValue] ? [UIColor clearColor] : [UIColor yellowColor]; }] subscribeNext:^(id _Nullable x) { self.passwordTF.backgroundColor = x; }]; // 4.宏来更好的完成上面的事情 RAC(self.passwordTF, backgroundColor) = [validPasseordSifnal map:^id _Nullable(id _Nullable value) { return [value boolValue] ? [UIColor clearColor] : [UIColor yellowColor]; }]; RAC(self.userNameTF, backgroundColor) = [validUserNameSignal map:^id _Nullable(id _Nullable value) { return [value boolValue] ? [UIColor clearColor] : [UIColor yellowColor]; }]; } - (BOOL)isValidUsername:(NSString *)username { return username.length > 3; } - (BOOL)isValidPassword:(NSString *)password { return password.length > 3; }
5.登录按钮的点击进入判断条件:
// 5.聚合信号 登录按钮只有当用户名和密码输入框的输入都有效时才工作 RACSignal *signUpActiveSignal = [RACSignal combineLatest:@[validUserNameSignal ,validPasseordSifnal] reduce:^id (NSNumber *userNameValid, NSNumber *passwordValid) { return @([userNameValid boolValue] && [passwordValid boolValue]); }]; [signUpActiveSignal subscribeNext:^(id _Nullable x) { self.logInButton.enabled = [x boolValue]; }];
6.登录按钮触发:
[[self.logInButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) { NSLog(@"button Click---"); }];
[[[self.logInButton rac_signalForControlEvents:UIControlEventTouchUpInside] flattenMap:^id(id x){ return[self logginSignal]; }] subscribeNext:^(NSNumber*signedIn){ BOOL success =[signedIn boolValue]; if(success){ NSLog(@"成功登录"); ViewController1 *vc1 = [[ViewController1 alloc]init]; [self.navigationController pushViewController:vc1 animated:YES]; } }];
学了一些基础,慢慢学习吧!!! 需要源码的可以Q我哟!! 1299625033..哈哈
参考博文链接: http://www.cocoachina.com/ios/20150123/10994.html