How to add a list to search field dynamically?

518 views Asked by At

i want to impliment a functionality like when i click on search field i need to display a list of criteria under the search field. Based on the criteria i need to search. how can i add this list to search field. Thanks in advance.

1

There are 1 answers

5
Hussain Shabbir On BEST ANSWER

For dynamically appearing list in the SearchField, you have to drag and drop outlet of searchfield in your window first then connect the delegate of searchfield to fileowners once this is done then impelement below code:-

In this below code it contains one array of elements and after if you enter in search field any word whose length is greater than 3 then it will display the result list according to the matching words in an 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
}