我想监听一个UISearchBar的isFirstResponder的值的变化,采用了如下代码:
[_searchBar addObserver:self forKeyPath:@"_isFirstResponder" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"test");
return ;
}
在UISearchBar的delegate方法中可以看到isFirstResponder确定已经改变了,但是却没有输出"test",用searchBar.observationInfo也可以看到该监听器确实是已经添加成功了,请问谁有没有碰到过这种情况?
There is no such key as "_isFirstResponder"! You can judge it by valueForKey or valueForKeyPath! If you want to monitor this property, you can customize UISearchBar, override the becomeFirstResponder and resignFirstResponder methods, and use Notification or delegate or block to pass the message
It is recommended to learn about RAC. This can be easily solved with RAC.