我現在已經弄清楚如何過濾NSTreeController,做到這一點我有NSManagedObject子類,並添加了一些代碼到我的應用程序委託,我也綁定了我的NSSearchField到我的應用程序委託的filterPredicate但我想我需要以某種方式連接我的NSTreeController和NSSearchField以使其工作。 下面我已經發布了所有我已經使用過的代碼,試着讓它工作。過濾樹控制器
NSManagedObject子類頭文件。
@interface Managed_Object_Sub_Class : NSManagedObject {
NSArray *filteredChildren; // this should fix the compiler error
}
- (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:@"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
代碼添加到應用程序委託頭文件
NSPredicate *filterPredicate;
代碼添加到應用程序委託實現文件
- (NSPredicate *)filterPredicate {
return filterPredicate;
}
- (void)setFilterPredicate:(NSPredicate *)newFilterPredicate {
if (filterPredicate != newFilterPredicate) {
[filterPredicate release];
filterPredicate = [newFilterPredicate retain];
}
}
搜索字段綁定
alt text http://snapplr.com/snap/vs9q
這還沒有工作,所以這就是爲什麼我問什麼,我需要在這裏做的,使其工作,就像我說的,我想我需要連接NSSearchField和NSTreeController以某種方式結合在一起。