RAC常用用法:

1.监听按钮的点击事件:

 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(, , , );
[button setTitle:@"点击事件" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
[self.view addSubview:button]; [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"按钮被点击了");
}];

2.代理的使用:

@interface RedView : UIView

//*  */
@property (nonatomic, strong) RACSubject *btnClickSignal; @end
#import "RedView.h"

@implementation RedView
- (void)awakeFromNib { [super awakeFromNib];
} //懒加载信号
- (RACSubject *)btnClickSignal { if (_btnClickSignal == nil) {
_btnClickSignal = [RACSubject subject];
}
return _btnClickSignal;
} - (IBAction)btnClick:(UIButton *)sender { [self.btnClickSignal sendNext:@"按钮被点击了"];
} @end
 // 只要传值,就必须使用RACSubject
RedView *redView = [[[NSBundle mainBundle] loadNibNamed:@"RedView" owner:nil options:nil] lastObject];
redView.frame = CGRectMake(, , self.view.bounds.size.width, );
[self.view addSubview:redView];
[redView.btnClickSignal subscribeNext:^(id x) {
NSLog(@"---点击按钮了,触发了事件");
}]; // 把控制器调用didReceiveMemoryWarning转换成信号
//rac_signalForSelector:监听某对象有没有调用某方法 [[self rac_signalForSelector:@selector(didReceiveMemoryWarning)] subscribeNext:^(id x) {
NSLog(@"控制器调用didReceiveMemoryWarning"); }];

3.KVO

把监听redV的center属性改变转换成信号,只要值改变就会发送信号

 // observer:可以传入nil
[[redView rac_valuesAndChangesForKeyPath:@"center" options:NSKeyValueObservingOptionNew observer:nil] subscribeNext:^(id x) {
NSLog(@"center:%@",x);
}];

4.代替通知

UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(, , , )];
textfield.placeholder = @"监听键盘的弹起";
textfield.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textfield]; [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(id x) {
NSLog(@"监听键盘的高度:%@",x);
}];

5.监听文字的改变

   [textfield.rac_textSignal subscribeNext:^(id x) {

        NSLog(@"输入框中文字改变了---%@",x);
}];

6.处理多个请求,都返回结果的时候,统一做处理.

 RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

        // 发送请求1
[subscriber sendNext:@"发送请求1"];
return nil;
}]; RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// 发送请求2
[subscriber sendNext:@"发送请求2"];
return nil;
}]; // 使用注意:几个信号,参数一的方法就几个参数,每个参数对应信号发出的数据。
[self rac_liftSelector:@selector(updateUIWithR1:r2:) withSignalsFromArray:@[request1,request2]]; // 更新UI
- (void)updateUIWithR1:(id)data r2:(id)data1
{
NSLog(@"更新UI%@ %@",data,data1);
}

 

iOS开发ReactiveCocoa学习笔记(三)的更多相关文章

  1. iOS开发ReactiveCocoa学习笔记(六)

    RAC操作方法三. demo地址:https://github.com/SummerHH/ReactiveCocoa.git doNext deliverOn timeout interval del ...

  2. iOS开发ReactiveCocoa学习笔记(一)

    学习 RAC 我们首先要了解 RAC 都有哪些类 RACSignal RACSubject RACSequence RACMulticastConnection RACCommand 在学习的时候写了 ...

  3. iOS开发ReactiveCocoa学习笔记(五)

    ReactiveCocoa常见操作方法介绍: demo地址:https://github.com/SummerHH/ReactiveCocoa.git filter ignore ignoreValu ...

  4. iOS开发ReactiveCocoa学习笔记(四)

    ReactiveCocoa常见操作方法介绍: demo地址:https://github.com/SummerHH/ReactiveCocoa.git 1.1 ReactiveCocoa操作须知 所有 ...

  5. iOS开发ReactiveCocoa学习笔记(二)

    RAC 中常见的宏: 使用宏定义要单独导入 #import <RACEXTScope.h> 一. RAC(TARGET, [KEYPATH, [NIL_VALUE]]):用于给某个对象的某 ...

  6. ios开发swift学习第三天:逻辑分支

    一. 分支的介绍 分支即if/switch/三目运算符等判断语句 通过分支语句可以控制程序的执行流程 二. if分支语句 和OC中if语句有一定的区别 判断句可以不加() 在Swift的判断句中必须有 ...

  7. ios开发网络学习AFN三:AFN的序列化

    #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...

  8. iOS开发RunLoop学习:三:Runloop相关类(source和Observer)

    一:RunLoop相关类: 其中:source0指的是非基于端口por,说白了也就是处理触摸事件,selector事件,source1指的是基于端口的port:是处理系统的一些事件 注意:创建一个Ru ...

  9. VSTO学习笔记(三) 开发Office 2010 64位COM加载项

    原文:VSTO学习笔记(三) 开发Office 2010 64位COM加载项 一.加载项简介 Office提供了多种用于扩展Office应用程序功能的模式,常见的有: 1.Office 自动化程序(A ...

随机推荐

  1. 最近一直是web前段,没什么意思,所以就不发资料了

    最近一直是web前段,没什么意思,所以就不发资料了 版权声明:本文为博主原创文章,未经博主允许不得转载.

  2. Spring pom配置详解(转)

    转载至http://blog.csdn.net/ithomer/article/details/9332071# 原博主注释的很详细 <project xmlns="http://ma ...

  3. Java静态检测工具/Java代码规范和质量检查简单介绍(转)

    静态检查: 静态测试包括代码检查.静态结构分析.代码质量度量等.它可以由人工进行,充分发挥人的逻辑思维优势,也可以借助软件工具自动进行.代码检查代码检查包括代码走查.桌面检查.代码审查等,主要检查代码 ...

  4. ES Docs-2:Exploring ES cluster

    The REST API Now that we have our node (and cluster) up and running, the next step is to understand ...

  5. 如何保持blog的高质量(相对于自己的进步而言的)

    多写! 多改!! 多删!!!

  6. VS(Visual Studio)中快速找出含中文的字符串

    环境:visual studio 2017 1.ctrl + shift + f 打卡全局查找 2.输入(".*[\u4E00-\u9FA5]+)|([\u4E00-\u9FA5]+.*&q ...

  7. 51nod1419 【数学】

    思路: n<=3,就是n. 考虑n>3: 我们可以轻松证明n,n-1这两个数互质: 设gcd(n,n-1)=g,n=g*k1,n-1=g*k2; n-(n-1)=g(k1-k2)=1; 所 ...

  8. uva 1608 不无聊的序列

    uva 1608 不无聊的序列 紫书上有这样一道题: 如果一个序列的任意连续子序列中都至少有一个只出现一次的元素,则称这个序列时不无聊的.输入一个n个元素的序列,判断它是不是无聊的序列.n<=2 ...

  9. 2017-10-15 NOIP模拟赛

    Stack #include<iostream> #include<cstdio> #define mod 7 using namespace std; ][],n; int ...

  10. CF987A Infinity Gauntlet 模拟

    You took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infini ...