我在XCode 9.3,objective-c,OSX而不是iOS。
我在我的应用程序中使用NSPredicateEditor,到目前为止工作正常。但是我有一个视图,它将使用编辑器中设置的谓词更新其内容(基本上视图显示已过滤的数组)。
目前我有一个“刷新”按钮,用户在编辑器中更改内容后需要点击以更新视图。
我想知道是否有一种方法可以在添加或更改predicateRow时触发我的方法自动更新视图?
我试图将一个观察者添加到NSPredicateEditor.objectValue - 但我没有收到通知。
- (void)viewWillAppear {
[self.predicateEditor.objectValue addObserver:self selector:@selector(predicateChangedByUser:) name:@"Test" object:nil];
}
- (void)predicateChangedByUser:(NSNotification*)aNotification {
NSLog(@"Changed: %@",aNotification);
}
任何帮助赞赏
您没有收到通知,因为您正在尝试合并通知和KVO。一些解决方案
解决方案A:将谓词编辑器的操作连接到操作方法。
解决方案B:观察通知NSRuleEditorRowsDidChangeNotification
。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(predicateChangedByUser:) name:NSRuleEditorRowsDidChangeNotification object:self.predicateEditor];
- (void)predicateChangedByUser:(NSNotification *)notification {
NSLog(@"predicateChangedByUser");
}
解决方案C:观察谓词编辑器的keypath 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:将编辑器的值绑定到谓词属性。