2

検索フィールドをクリックしたときのような機能を実装したいのですが、検索フィールドの下に基準のリストを表示する必要があります。検索する必要がある基準に基づいています。このリストを検索フィールドに追加するにはどうすればよいですか。前もって感謝します。

4

1 に答える 1

0

にリストを動的に表示するには、最初にSearchFieldをドラッグ アンド ドロップし、これが完了したらのデリゲートを接続してから、コードの下に実装する必要があります。outletsearchfieldwindowsearchfieldfileowners

以下のコードでは、要素の配列が 1 つ含まれており、検索フィールドに長さが 3 より大きい単語を入力すると、一致する単語に従って結果リストが表示されますarray

-(void)controlTextDidChange:(NSNotification *)obj
{
NSArray *filterArray=@[@"item1",@"item2",@"item3",@"item4",@"test1",@"test2",@"test3",@"test4",@"test5"];
    NSString *searchString = [self.searchField stringValue];
    NSMenu *menu= [[NSMenu alloc] initWithTitle: @"results"];
    if (searchString.length>3)
    {
    NSArray *filteredArray = [filterArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF like  %@", [searchString stringByAppendingString:@"*"]]];
    for (NSString *addMenuItem in filteredArray)
    {
    [menu addItemWithTitle: addMenuItem action: @selector(someAction:) keyEquivalent: @""];
    }

    NSEvent *event = [NSEvent otherEventWithType: NSApplicationDefined
                                        location: [self.searchField frame].origin
                                   modifierFlags: 0
                                       timestamp: 0
                                    windowNumber: [[self.searchField window] windowNumber]
                                         context: [[self.searchField window] graphicsContext]
                                         subtype: NSAppKitDefined
                                           data1: 0
                                           data2: 0];
    [NSMenu popUpContextMenu: [menu autorelease] withEvent: event forView: self.searchField];
}
}
-(void)someAction:(id)sender
{
    //if you want to perform some action write here
}
于 2013-12-20T11:27:52.447 に答える