Теперь я почти понял, как фильтровать 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];
}
}
Привязка поля поиска
альтернативный текст http://snapplr.com/snap/vs9q
Это не работаеттем не менее, и именно поэтому я спрашиваю, что мне нужно сделать отсюда, чтобы заставить его работать, как я сказал, я думаю, что мне нужно каким-то образом соединить NSSearchField и NSTreeController.