给定N个文本字段,订阅UIControlEditingEventChanged事件,并根据被调用的值创建新信号.每个订户完成一次订阅,然后将这些值相互结合使用.所以这就是我所拥有的:
__block NSMutableSet *subscriptions = [NSMutableSet set];RACSignal *valIDationSignals = [[[[TEXT_FIELDS rac_sequence].signal flattenMap:^RACStream* (UITextFIEld *textFIEld) { return [textFIEld rac_signalForControlEvents:UIControlEventEditingChanged];}] flattenMap:^RACStream *(UITextFIEld* textFIEld) { if(![subscriptions containsObject:textFIEld]) { [subscriptions addobject:textFIEld]; return [textFIEld rac_valIDationSignal]; } return [RACSignal empty];}]];
其中rac_valIDationSignal是我自己的类处理UITextFIElds的验证.信号正确通过,但一次只能通过一个.我需要一次性获取所有订阅的值,以获得所有已编辑字段的有效性.
编辑:
一种可能的解决方案涉及一个可变数组,我将新字段添加到并按正常情况发送组合信号.这似乎相当不错,还有其他想法吗?
__block NSMutableSet *subscriptions = [NSMutableSet set];__block NSMutableArray *signals = [NSMutableArray array];RAC(self.editbutton,enabled) =[[[[TEXT_FIELDS rac_sequence].signal flattenMap:^RACStream* (UITextFIEld *textFIEld) { return [textFIEld rac_signalForControlEvents:UIControlEventEditingChanged];}] flattenMap:^RACStream *(UITextFIEld* textFIEld) { if(![subscriptions containsObject:textFIEld]) { [subscriptions addobject:textFIEld]; [signals addobject:[textFIEld rac_valIDationSignal]]; return [RACSignal combineLatest:signals]; } return [RACSignal empty];}] map:^NSNumber *(RACTuple *value) { for(NSNumber *isValID in value) { if(![isValID boolValue]) { return @(NO); } }return @(YES);}];
谢谢!
解决方法 你可以尝试这个:NSArray *TEXT_FIELDS = @[[txt1.rac_textSignal distinctUntilChanged],[txt2.rac_textSignal distinctUntilChanged]];[[RACSignal combineLastest:TEXT_FIELDS reduce:^ (Nsstring *txt1,Nsstring *txt2) { // Return your valIDate method here // Signal only send `next` value it is different from prevIoUs `next` value return [self inputvalication];}] setKeyPath:@"enabled" onObject:self.mybutton];总结
以上是内存溢出为你收集整理的objective-c – “结合最新”与ReactiveCocoa和信号信号全部内容,希望文章能够帮你解决objective-c – “结合最新”与ReactiveCocoa和信号信号所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)