2013-12-19 44 views
2

我想暗示功能就像當我點擊搜索字段時,我需要顯示搜索字段下的條件列表。基於我需要搜索的標準。我如何將這個列表添加到搜索字段中。 在此先感謝。如何動態添加列表到搜索字段?

+0

我已經更新了代碼。請看看它 –

回答

0

對於在SearchField動態出現的列表中,你必須拖動和下降的searchfieldoutletwindow第一則searchfield委託連接fileowners一旦做到這一點,然後教學貫徹下面的代碼: -

在這下面代碼它包含一個元素數組,如果在搜索字段中輸入任何長度大於3的單詞,則它將根據array中的匹配單詞顯示結果列表。

-(void)controlTextDidChange:(NSNotification *)obj 
{ 
NSArray *[email protected][@"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 
} 
+0

感謝您的回覆。但我的要求是,如果有假設,那裏有三個數組。我想顯示在搜索字段下方的3個數組名稱作爲列表..如果我將選擇其中的任何一個,那麼搜索應該發生在該特定的數組中。我需要如何顯示該數組列表? – Suneetha

+0

Ohk這也是可以的。但有一件事是那裏爲什麼你在這裏採取搜索。因爲在searchfield用戶可以鍵入單詞,然後在此基礎上搜索? –

+0

我只需要在那個特定的字段(數組)中搜索,只有用戶可以選擇。現在全球搜索正在我的代碼中發生。我不需要提供所有的結果,而是需要向用戶提供他們想要搜索的字段的選項。 – Suneetha