在 Day1 Day2 中介绍了RAC的常见用法,本篇将继续介绍RAC
在项目实战中,常用到的RAC
为UIKit
提供的信号即方法
1.KVO
- (RACSignal *)rac_valuesForKeyPath:(NSString *)keyPath observer:(__weak NSObject *)observer
2.UIControl
/UIButton
- 返回点击等事件的信号
- (RACSignal<__kindof UIControl *> *)rac_signalForControlEvents:(UIControlEvents)controlEvents;
3.UITextField
/UITextView
- text改变的信号
- (RACSignal<NSString *> *)rac_textSignal;
4.UITableViewCell
/UITableViewHeaderFooterView
- 返回
cell
准备进入复用池的信号,这个信号一般用于在cellforindexpath
方法中,避免cell
重复订阅信号
@property (nonatomic, strong, readonly) RACSignal<RACUnit *> *rac_prepareForReuseSignal;
5.NSNotificationCenter
好处是object
可选,如果为nil,则无需关心监听者释放问题(dealloc 无需移除通知监听)
- (RACSignal<NSNotification *> *)rac_addObserverForName:(nullable NSString *)notificationName object:(nullable id)object;
6.监听方法/代理和代替代理(仅限于无返回值的代理 比如UITableView
的tableView:didSelectRowAtIndexPath:
)
- 监听方法
- (RACSignal<RACTuple *> *)rac_signalForSelector:(SEL)selector;
- 这个是单独用来监听代理方法的
- (RACSignal<RACTuple *> *)rac_signalForSelector:(SEL)selector fromProtocol:(Protocol *)protocol;
e.g:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.blueColor;
_tbView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tbView.frame = self.view.bounds;
[self.view addSubview:_tbView];
// ❌如果先于rac_signalForSelector设置代理或导致信号订阅失效
//原因=》在设置代理的时候,系统会缓存这个代理对象实现了哪些代码方法
//如果将代理放在订阅信号前设置,那么当控制器成为代理时是无法缓存这个代理对象实现了哪些代码方法的
// _tbView.delegate = self;
// _tbView.dataSource = self;
[[self rac_signalForSelector:@selector(tableView:cellForRowAtIndexPath:) fromProtocol:@protocol(UITableViewDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"tableView:cellForRowAtIndexPath:");
}];
[[self rac_signalForSelector:@selector(tableView:numberOfRowsInSection:) fromProtocol:@protocol(UITableViewDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"tableView:numberOfRowsInSection:");
}];
[[self rac_signalForSelector:@selector(tableView:didSelectRowAtIndexPath:) fromProtocol:@protocol(UITableViewDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"UITableViewDelegate:tableView:didSelectRowAtIndexPath:");
}];
[_tbView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
// ✅
_tbView.delegate = self;
_tbView.dataSource = self;
}
7.RACSubject
RACReplaySubject
RACSubject
继承自RACSignal
- 比起
RACSignal
,添加了一个可以主动发送信息的方法,要知道RACSignal
是不能主动调用sendNext
的
- (void)sendNext:(nullable ValueType)value;
//创建信号 RACSubject
RACSubject *subject = [RACSubject subject];
//没订阅前直接发送信号
[subject sendNext:@"subject 1"];
//订阅信号
[subject subscribeNext:^(id _Nullable x) {
NSLog(@" subject subscribeNext = %@", x);
}];
//发送信号
[subject sendNext:@"subject"];
//RACReplaySubject继承自RACSubject,区别是会订阅到曾经发送过的所有信号值
RACReplaySubject *replaySubject = [RACReplaySubject subject];
//没订阅前直接发送信号 在订阅后依然会收到
[replaySubject sendNext:@"replaySubject"];
[replaySubject subscribeNext:^(id _Nullable x) {
NSLog(@" replaySubject subscribeNext = %@", x);
}];
[replaySubject sendNext:@"replaySubject"];
还有其他用法后续会继续更新
网友评论