过滤树控制器 [英] Filtering A Tree Controller

查看:80
本文介绍了过滤树控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经弄清楚如何过滤一个NSTreeController,为此我有子类NSManagedObject并添加了一些代码到我的应用程序委托,我也绑定我的NSSearchField到我的应用程序代理的filterPredicate,但我认为需要连接我的NSTreeController和NSSearchField在某种程度上,使其工作。
下面我已经发布了所有的代码,我已经使用到目前为止尝试,并使其工作。






strong> NSManagedObject子类头文件。

  @interface Managed_Object_Sub_Class:NSManagedObject {
NSArray * filteredChildren ; //这应该修复编译器错误
}

- (NSArray *)filteredChildren;


@end

NSManagedObject子类实现文件

  @implementation Managed_Object_Sub_Class 

static char * FilteredChildrenObservationContext;

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context {
if(self = [super initWithEntity:entity insertIntoManagedObjectContext:context]){
[[NSApp delegate] addObserver:self forKeyPath:@filterPredicateoptions:0 context:& FilteredChildrenObservationContext];
[self addObserver:self forKeyPath:@subGroupoptions:0 context:& FilteredChildrenObservationContext];
}
return self;
}

//使用finalize和GC
- (void)dealloc {
[[NSApp delegate] removeObserver:self forKeyPath:@filterPredicate];
[self removeObserver:self forKeyPath:@subGroup];
[super dealloc]
}

- (NSArray *)filteredChildren {
if(filteredChildren == nil){
NSPredicate * predicate = [[NSApp delegate] filterPredicate];
if(predicate)
filteredChildren = [[self valueForKey:@subGroup] filteredArrayUsingPredicate:predicate] copy];
else
filteredChildren = [[self valueForKey:@subGroup] copy];
}
return filteredChildren;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context ==& FilteredChildrenObservationContext){
[self willChangeValueForKey:@filteredChildren];
[filteredChildren release];
filteredChildren = nil;
[self didChangeValueForKey:@filteredChildren];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

@end

strong>添加到应用程序代理头文件的代码

  NSPredicate * filterPredicate; 

添加到应用代理实施文件的代码

   - (NSPredicate *)filterPredicate {
return filterPredicate;
}

- (void)setFilterPredicate:(NSPredicate *)newFilterPredicate {
if(filterPredicate!= newFilterPredicate){
[filterPredicate release];
filterPredicate = [newFilterPredicate retain];
}
}

搜索字段绑定 / p>






这还没有工作,所以这就是为什么我问我从这里需要做什么,使其工作,像我说我认为我需要连接NSSearchField和NSTreeController以某种方式在一起。

解决方案

再次我hav回答了我自己的问题,我也希望这将帮助其他因此他们知道如何过滤一个NSTreeController。



要使它的工作从我的帖子上面做以下。



1.对于您的实体,将类设置为我的Case JGManagedObject中的NSManagedObject子类。






I have now nearly figured out how to Filter a NSTreeController, to do this I have sub-classed NSManagedObject and added some code to my App Delegate, I have also bound my NSSearchField to the filterPredicate of my App Delegate but I think I need to connect my NSTreeController and NSSearchField in some way to make it work. Below I have posted all the code I have used so far to try and make it work.


NSManagedObject Sub-Class Header File.

@interface Managed_Object_Sub_Class : NSManagedObject {
    NSArray *filteredChildren; // this should fix the compiler error
}

- (NSArray *)filteredChildren;


@end

NSManagedObject Sub-Class Implementation File.

@implementation Managed_Object_Sub_Class

static char *FilteredChildrenObservationContext;

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context {
    if (self = [super initWithEntity:entity insertIntoManagedObjectContext:context]) {
        [[NSApp delegate] addObserver:self forKeyPath:@"filterPredicate" options:0 context:&FilteredChildrenObservationContext];
        [self addObserver:self forKeyPath:@"subGroup" options:0 context:&FilteredChildrenObservationContext];
    }
    return self;
}

// use finalize with GC
- (void)dealloc {
    [[NSApp delegate] removeObserver:self forKeyPath:@"filterPredicate"];
    [self removeObserver:self forKeyPath:@"subGroup"];
    [super dealloc];
}

- (NSArray *)filteredChildren {
    if (filteredChildren == nil) {
        NSPredicate *predicate = [[NSApp delegate] filterPredicate];
        if (predicate)
            filteredChildren = [[[self valueForKey:@"subGroup"] filteredArrayUsingPredicate:predicate] copy];
        else
            filteredChildren = [[self valueForKey:@"subGroup"] copy];
    }
    return filteredChildren;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == &FilteredChildrenObservationContext) {
        [self willChangeValueForKey:@"filteredChildren"];
        [filteredChildren release];
        filteredChildren = nil;
        [self didChangeValueForKey:@"filteredChildren"];
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

@end

Code Added To App Delegate Header File

NSPredicate *filterPredicate;

Code Added To App Delegate Implementation File

- (NSPredicate *)filterPredicate {
    return filterPredicate;
}

- (void)setFilterPredicate:(NSPredicate *)newFilterPredicate {
    if (filterPredicate != newFilterPredicate) {
        [filterPredicate release];
        filterPredicate = [newFilterPredicate retain];
    }
}

Search Field Binding


This doesn't work yet, and so that is why I am asking what I need to do from here to make it work, like I said I think I need to connect the NSSearchField and NSTreeController Together in some way.

解决方案

Again I hav answered my own question, I also hope that this will help other people so they know how to Filter a NSTreeController.

To make it work from my post above do the following.

1.For your entity set the Class as your NSManagedObject Sub-Class in my Case JGManagedObject.

2.For your search field in IB set the predicate format to what you want to Filter ( The Property in your entity, for me it is name).

这篇关于过滤树控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆