开发中ReactiveCocoa(RAC)常见用法
RAC监听按钮点击事件
[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"RAC检测按钮点击");
}];
self.button.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
NSLog(@"RAC检测按钮点击2");
return [RACSignal empty];
}];
RAC监听手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
[[tap rac_gestureSignal] subscribeNext:^(id x) {
NSLog(@"RAC检测手势");
}];
self.imageView.userInteractionEnabled = YES;
[self.imageView addGestureRecognizer:tap];
RAC遍历数组 / 字典
NSArray *array = @[@1,@3,@55,@76,@56,@45];
[array.rac_sequence.signal subscribeNext:^(id x) {
NSLog(@"RAC遍历数组元素:%@",x);
}];
NSDictionary *dict = @{@"name":@"stevin",@"location":@"Beijing"};
[dict.rac_sequence.signal subscribeNext:^(RACTuple *x) {
RACTupleUnpack(NSString *key,NSString *value) = x;
NSLog(@"RAC遍历字典键值:%@--%@",key,value);
}];
RAC观察值改变
[RACObserve(self,string) subscribeNext:^(NSString *string) {
NSLog(@"RAC检测值改变:%@",string);
}];
RAC监听UITextField输入
[self.textFieldName.rac_textSignal subscribeNext:^(id x) {
NSLog(@"RAC监听TextField输入:%@",x);
}];
[[self.textFieldName.rac_textSignal filter:^BOOL(id value) {
NSString *text = value;
return text.length > 3;
}] subscribeNext:^(id x) {
NSLog(@"RAC检测到输入了长度大于3的内容:%@",x);
}];
RAC代替代理Delegate
[[self rac_signalForSelector:@selector(tableViewCell:buttonClick:) fromProtocol:@protocol(TableViewCellDelegate)] subscribeNext:^(id x) {
NSLog(@"RAC代理响应成功");
}];
RAC观察通知Notification
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:NotificationName object:nil] subscribeNext:^(NSNotification *notification) {
NSLog(@"RAC通知响应成功:%@",notification.userInfo);
}];
[[self rac_signalForSelector:@selector(tableView:numberOfRowsInSection:)] subscribeNext:^(id x) {
NSLog(@"tableView:numberOfRowsInSection:被调用!");
}];