2009-08-05 5 views
1

私は今、ほぼ、NSTreeControllerをフィルタリングするために、私は、サブ分類NSManagedObjectと私のアプリデリゲートにいくつかのコードを追加して、私も私のアプリの委任のfilterPredicateに私のNSSearchFieldを拘束しているしているこれを行う方法を考え出しました私はNSTreeControllerとNSSearchFieldを何らかの方法で接続して動作させる必要があると思います。 以下、これまでに使用していたすべてのコードを掲載して動作させました。フィルタリングAツリーは、コントローラ


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一緒に何らかの形で。

答えて

1

もう一度自分の質問に答えました。これは他の人に役立ち、NSTreeControllerをフィルタリングする方法を知っていることを願っています。

私のポストから機能させるには、次のようにします。

1.あなたのエンティティは、私のケースJGManagedObjectのNSManagedObjectサブクラスとしてクラスを設定します。

alt text http://dvlp.me/c3k

IBに検索フィールドを使用して、フィルタしたいものに述語形式を設定2.For(エンティティにプロパティを、私のためにそれは名前です)。

alt text http://dvlp.me/9k9rw

関連する問題