我上的是XCode 9.3,目标-c,OSX不是iOS。
我在我的应用程序中使用了一个NSPredicateEditor,到目前为止,它运行得很好。但是,我有一个视图,它将使用编辑器中设置的谓词更新其内容(基本上,视图显示过滤的数组)。
目前,我有一个“刷新”按钮,用户需要点击更新视图,一旦他在编辑器中改变了一些东西。
我想知道是否有一种方法可以在predicateRow is 添加或changed时自动更新视图
我试图在NSPredicateEditor.objectValue中添加一个观察者--但是我没有收到通知。
- (void)viewWillAppear {
[self.predicateEditor.objectValue addObserver:self selector:@selector(predicateChangedByUser:) name:@"Test" object:nil];
}
- (void)predicateChangedByUser:(NSNotification*)aNotification {
NSLog(@"Changed: %@",aNotification);
}感谢你的任何帮助
发布于 2018-03-03 22:17:47
您没有收到通知,因为您试图将通知和KVO结合起来。一些解决办法:
解决方案A:将谓词编辑器的操作连接到操作方法。
解决方案B:观察通知NSRuleEditorRowsDidChangeNotification。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(predicateChangedByUser:) name:NSRuleEditorRowsDidChangeNotification object:self.predicateEditor];
- (void)predicateChangedByUser:(NSNotification *)notification {
NSLog(@"predicateChangedByUser");
}解决方案C:观察谓词编辑器的键盘predicate。predicate是NSRuleEditor的一个属性。
static void *observingContext = &observingContext;
[self.predicateEditor addObserver:self forKeyPath:@"predicate" options:0 context:&observingContext];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == &observingContext)
NSLog(@"observeValueForKeyPath %@", keyPath);
else
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}解决方案D:将编辑器的值绑定到谓词属性。
发布于 2020-01-12 09:18:41
“NSPredicateEditor”有一个“action”选择器,可以在代码中或通过使用接口设计器中的出口连接到函数,如下所示:
- (IBAction)predicateChanged:(id)sender {
// Update your view
}https://stackoverflow.com/questions/49087289
复制相似问题