首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSPredicateEditor callBack添加或更改NSPredicate时

NSPredicateEditor callBack添加或更改NSPredicate时
EN

Stack Overflow用户
提问于 2018-03-03 17:49:47
回答 2查看 186关注 0票数 0

我上的是XCode 9.3,目标-c,OSX不是iOS。

我在我的应用程序中使用了一个NSPredicateEditor,到目前为止,它运行得很好。但是,我有一个视图,它将使用编辑器中设置的谓词更新其内容(基本上,视图显示过滤的数组)。

目前,我有一个“刷新”按钮,用户需要点击更新视图,一旦他在编辑器中改变了一些东西。

我想知道是否有一种方法可以在predicateRow is 添加changed时自动更新视图

我试图在NSPredicateEditor.objectValue中添加一个观察者--但是我没有收到通知。

代码语言:javascript
复制
- (void)viewWillAppear {
    [self.predicateEditor.objectValue addObserver:self selector:@selector(predicateChangedByUser:) name:@"Test" object:nil];
}

- (void)predicateChangedByUser:(NSNotification*)aNotification {
    NSLog(@"Changed: %@",aNotification);
}

感谢你的任何帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-03 22:17:47

您没有收到通知,因为您试图将通知和KVO结合起来。一些解决办法:

解决方案A:将谓词编辑器的操作连接到操作方法。

解决方案B:观察通知NSRuleEditorRowsDidChangeNotification

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(predicateChangedByUser:) name:NSRuleEditorRowsDidChangeNotification object:self.predicateEditor];

- (void)predicateChangedByUser:(NSNotification *)notification {
    NSLog(@"predicateChangedByUser");
}

解决方案C:观察谓词编辑器的键盘predicatepredicateNSRuleEditor的一个属性。

代码语言:javascript
复制
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:将编辑器的值绑定到谓词属性。

票数 0
EN

Stack Overflow用户

发布于 2020-01-12 09:18:41

“NSPredicateEditor”有一个“action”选择器,可以在代码中或通过使用接口设计器中的出口连接到函数,如下所示:

代码语言:javascript
复制
- (IBAction)predicateChanged:(id)sender {
    // Update your view
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49087289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档