ReactiveCocoa
很早就开始关注ReactiveCocoa了,前段时间决定把它加入到项目中,理由如下
从开始了解 @weakify(self); [[self.nextButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) { @strongify(self); [self performSegueWithIdentifier:@"Captcha" sender:nil]; }]; 这样单一的信号传递和部署,没有和其他信号有任何的联系,总感觉和 先说说很常用的验证码倒计时功能,用RAC来实现几乎一气呵成 @weakify(self); RACSignal *timeSignal = [[[[[RACSignal interval:1.0f onScheduler:[RACScheduler mainThreadScheduler]] take:numberLimit] startWith:@(1)] map:^id(NSDate *date) { @strongify(self); if (number == 0) { [self.timeButton setTitle:@"重新发送" forState:UIControlStateNormal]; return @YES; } else{ self.timeButton.titleLabel.text = [NSString stringWithFormat:@"%d",number--]; return @NO; } }] takeUntil:self.rac_willDeallocSignal]; self.timeButton.rac_command = [[RACCommand alloc]initWithEnabled:timeSignal signalBlock:^RACSignal *(id input) { number = numberLimit; return timeSignal; }];
如果RAC利用得当,几乎可以抛弃写自定义的委托协议。想象一下:当aVC 其中的关键方法是 [[self rac_signalForSelector:@selector(dismiss:)] subscribeNext:^(id x) { NSLog(@"%s",__func__); }]; - (IBAction)dismiss:(id)sender{ [self dismissViewControllerAnimated:YES completion:NULL]; } 当 信号什么时候部署可以由我们来决定,既然需要在aVC中处理一些事,那么就应该想办法在aVC中来部署信号。这时候就需要把信号作为bVC的属性
#import <UIKit/UIKit.h> @interface RACDelegateViewController : UIViewController @property (nonatomic,strong) RACSignal *delegateSignal; @end 部署信号
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([segue.destinationViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *nav = (UINavigationController *)segue.destinationViewController; RACDelegateViewController *delegateVC = (RACDelegateViewController *)nav.topViewController; [delegateVC.delegateSignal subscribeNext:^(id x) { NSLog(@"%@",x); }]; } } 还有最后一步,生成
- (void)awakeFromNib{ [super awakeFromNib]; self.delegateSignal = [self rac_signalForSelector:@selector(dismiss:)]; } 注意:千万不要在 还不够完美对不对,aVC中接收的数据一直都是UIBarButtonItem对象,想把bVC中的数据传递到aVC中应该如何实现。还必须在信号上做一些调整,利用 - (void)awakeFromNib{ [super awakeFromNib]; @weakify(self); self.delegateSignal = [[self rac_signalForSelector:@selector(dismiss:)] then:^RACSignal *{ @strongify(self); return [RACSignal return:self.array]; }]; } 当 - (void)awakeFromNib{ [super awakeFromNib]; @weakify(self); self.delegateSignal = [[self rac_signalForSelector:@selector(dismiss:)] map:^id(id value) { @strongify(self); return self.array; }]; } 对于需要监听协议方法的时候可以使用 前段时间看了
- (id)initWithCellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(CellConfigureBlock)aConfigureCellBlock{ self = [super init]; if (self) { _cellIdentifier = aCellIdentifier; _configureCellBlock = [aConfigureCellBlock copy]; _signal = RACObserve(self,dataSource); } return self; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_cellIdentifier forIndexPath:indexPath]; if (self.configureCellBlock) { @weakify(self); self.configureCellBlock(cell,[self.signal map:^id(NSArray *array) { @strongify(self); return [self itemAtIndexPath:indexPath]; }]); } return cell; }
self.model = [[DataSourceGeneralModel alloc] initWithCellIdentifier:CelIdentifier configureCellBlock:^(TableViewCell *cell,RACSignal *signal) { [cell configureCellWithSignal:signal]; }];
- (void)configureCellWithSignal:(RACSignal *)signal{ @weakify(self); [signal subscribeNext:^(Model *model) { @strongify(self); self.titleLabel.text = model.title; self.detailLabel.text = model.detail; }]; } 如果更新数据源数组,UITableView也会得到相应的更新,不用调用 self.model.dataSource = @[model1]; 以上例子可以在github下载 待续..........RAC相关博客
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |