RAC(ReactiveCocoa)小记-新人必看
一、配置RAC环境 :
platform:ios, '8.0'
pod 'ReactiveCocoa','~>2.1.8'
二、RAC能干什么?
1、对事件的监听
例如我们监听一个textfield输入完成的事件,按照常规思路我们需要做如下操作:
//factoryTextField是一个工具方法,直接返回UITextField对象
self.textField = [FactoryTool factoryTextField:@"请输入" color:[UIColor blueColor]size:18];
self.textField.delegate = self;
self.textField.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.textField];
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.height.equalTo(@30);
make.width.equalTo(self.view.mas_width).multipliedBy(0.5);
}];
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"%@",textField.text);
}
通过代理UITextField的代理方法 我们来获取到用户输入完成
textFieldDidEndEditing 从而达到监听UITextField的需求。
如果我们使用RAC的监听方法,则只要2行代码搞定:
[[self.textField rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x){
//x是textField对象
NSLog(@"%@",x);
}];
或者使用下面的更加简单的方法:
[[self.textField rac_textSignal]subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
备注:后面的方法相当于是监听了 UIControlEventEditingChanged 在textfield值更改一次后,就会调用一次这个block方法。
RAC还可以监听tap事件,假如我们就在当前控制的View上增加一个tap事件:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
[[tap rac_gestureSignal]subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
[self.view addGestureRecognizer:tap];
在每次tap当时view时,block就会执行一次,是不是感觉方便很多,不用写自定义方法.啦.
当然RAC还可以监听其他控件,比如比较常用的uibutton等等,经过测试 只要是UIControlEvents 枚举的都可以被监听,可自行去测试。
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown = 1 << 0, // on all touch downs
UIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1)
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12, // sliders, etc.
UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semantic action: for buttons, etc.
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
UIControlEventAllTouchEvents = 0x00000FFF, // for touch events
UIControlEventAllEditingEvents = 0x000F0000, // for UITextField
UIControlEventApplicationReserved = 0x0F000000, // range available for application use
UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use
UIControlEventAllEvents = 0xFFFFFFFF
};
2、RAC 执行UIAlertView代理方法 只能执行无返回值的代理方法,所以要慎用!
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"RAC" message:@"RAC TEST" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];
[alertView show];
[[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)] subscribeNext:^(RACTuple *tuple) {
NSLog(@"%@",tuple.first);
NSLog(@"%@",tuple.second);
NSLog(@"%@",tuple.third);
}];
上面的Block可以换成下面的2行代码搞定:
[[alertView rac_buttonClickedSignal]subscribeNext:^(id x) {
NSLog(@"%@",x);//可以拿到alertView每个index的值
}];
3、RAC处理通知
我们在某个控制器直接发送一个通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"RACNotification" object:@[@"1",@"2"]];
在接收"RACNotification"的控制器中可轻松拿到通知传的值
[[[NSNotificationCenter defaultCenter]rac_addObserverForName:@"RACNotification" object:nil]subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
此处的id x 的id 类型就是NSNotification的类型,可以使用NSNotification来代替..
RAC的通知有一个很棒的地方,add的通知,我们不需要再dealloc中进行移除,对于懒人比较适合....
三、啥时候使用RAC
网上一个大牛说的一句话:只要用到add target时 都可以用RAC来代替,秒懂了哇!
1、欢迎来到Biny博客!
2、专注于iOS移动端开发,欢迎广大大师们、大神们吐槽.....
3、欢迎友情链接 网站名:Biny博客 网址:http://www.Xcode.cc 描述:Biny博客是一个技术博客,文章主要包含移动互联IOS相关内容,记录一些在IOS编码过程中遇到的坑和看到大神们记录下的经验,以便查阅。