iOS ReactiveCocoa

来源:互联网 发布:项羽跟虞姬 知乎 编辑:程序博客网 时间:2024/06/10 23:00

一、ReactiveCocoa简介

ReactiveCocoa框架(简称RAC)是Github上的一个开源项目,是一个将函数响应式编程范例带入Object-C的开源库。iOS开发中的事件包括:Target、Delegate、KVO、通知、时钟、网络异步回调。ReactiveCocoa ,就是用信号接管了iOS 中的所有事件;也就意味着,用一种统一的方式来处理iOS中的所有事件,解决了各种分散的事件处理方式。

二、ReactiveCocoa的使用

2.1 KVO监听

注意,RAC 的信号一旦注册不会主动释放

只要在 block 中包含有 self. 一定会出现强引用* 需要使用 @weakify(self) / @strongify(self) 组合使用解除强引用

#import "ViewController.h"#import <ReactiveCocoa/ReactiveCocoa.h>@interface ViewController ()@property (nonatomic, assign) NSInteger num;@property (nonatomic, strong) UILabel *label;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 50, self.view.frame.size.width-40,  35)];    [button setTitle:@"测试" forState:UIControlStateNormal];    [button setBackgroundColor:[UIColor orangeColor]];    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];    _label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30)];    [self.view addSubview:_label];    [self testKVO];}- (void)buttonClick{    if (self.num < 10) {        self.num ++;    }}#pragma mark - KVO 监听- (void)testKVO{    @weakify(self)    [RACObserve(self, self.num) subscribeNext:^(id x) {        @strongify(self)        self.label.text = [NSString stringWithFormat:@"当前num的值为:%ld", (long)self.num];    }];}

2.2 文本输入框事件监听

- (void)demoTextField {    @weakify(self);    [[self.nameText rac_textSignal]     subscribeNext:^(id x) {         @strongify(self);         NSLog(@"%@",x);         self.person.name = x;     }];}

2.3 文本框组合信号

- (void)textFileCombination {    id signals = @[[self.nameText rac_textSignal],[self.passWordText rac_textSignal]];    @weakify(self);    [[RACSignal      combineLatest:signals]      subscribeNext:^(RACTuple *x) {          @strongify(self);          NSString *name = [x first];          NSString *password = [x second];          if (name.length > 0 && password.length > 0) {              self.loginButton.enabled = YES;              self.person.name = name;              self.person.password = password;          } else  {              self.loginButton.enabled = NO;          }      }];}

2.4 按钮监听

#pragma -mark 按钮监听- (void)buttonDemo {    @weakify(self);    [[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside]       subscribeNext:^(id x) {           @strongify(self);           NSLog(@"person.name:  %@    person.password:  %@",self.person.name,self.person.password);       }     ];}

2.5 代理方法

#pragma -mark 代理方法/** * 5、验证此函:nameText的输入字符时,输入回撤或者点击键盘的回车键使passWordText变为第一响应者(即输入光标移动到passWordText处) */- (void)delegateDemo {    @weakify(self)    // 1. 定义代理    self.proxy = [[RACDelegateProxy alloc] initWithProtocol:@protocol(UITextFieldDelegate)];    // 2. 代理去注册文本框的监听方法    [[self.proxy rac_signalForSelector:@selector(textFieldShouldReturn:)]     subscribeNext:^(id x) {         @strongify(self)         if (self.nameText.hasText) {             [self.passWordText becomeFirstResponder];         }     }];    self.nameText.delegate = (id<UITextFieldDelegate>)self.proxy;}

2.6 通知

#pragma -mark 通知/** * 验证此函数:点击textFile时,系统键盘会发送通知,打印出通知的内容 */- (void)notificationDemo {    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil]        subscribeNext:^(id x) {            NSLog(@"notificationDemo : %@", x);        }     ];}
0 0
原创粉丝点击