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];//1>把數組轉換成集合RACSequence array.rac_sequence//2>把集合RACSequence轉換RACSignal信號類,array.rac_sequence.signal//3>訂閱信號,激活信號,會自動把集合中的所有值,遍歷出來。
[array.rac_sequence.signal subscribeNext:^(id x) {
? ? NSLog(@"RAC遍歷數組元素:%@",x);
}];
//遍歷字典NSDictionary *dict = @{@"name":@"stevin",@"location":@"Beijing"};
//RACTuple:元組類,類似NSArray,用來包裝值.
//RACSequence:RAC中的集合類,用于代替NSArray,NSDictionary,可以使用它來快速遍歷數組和字典。
[dict.rac_sequence.signal subscribeNext:^(RACTuple *x) {
? ? ? ? ?//解包元組,會把元組的值,按順序給參數里面的變量賦值? ? RACTupleUnpack(NSString *key, ? ? ? ? ? ? ? ? ? ?NSString *value) = x;
? ? NSLog(@"RAC遍歷字典鍵值:%@--%@",key,value);
}];
RAC觀察值改變
//監聽本類屬性string值得變化并打印
[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;
? ? //只有當文本框的內容長度大于3,才獲取文本框里的內容? ? return text.length > 3;
}] subscribeNext:^(id x) {
? ? NSLog(@"RAC檢測到輸入了長度大于3的內容:%@",x);
}];
RAC代替代理Delegate
[[self rac_signalForSelector:@selector(tableViewCell:buttonClick:) fromProtocol:@protocol(TableViewCellDelegate)]subscribeNext:^(idx) {?
? ???NSLog(@"RAC代理響應成功");
}];
RAC觀察通知Notification
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:NotificationName object:nil] subscribeNext:^(NSNotification *notification) {
? ? ????NSLog(@"RAC通知響應成功:%@",notification.userInfo);
}];
RAC監聽方法被調用
[[self rac_signalForSelector:@selector(tableView:numberOfRowsInSection:)] subscribeNext:^(id x) {
? ? ????NSLog(@"tableView:numberOfRowsInSection:被調用!");
}];