我想实现一个功能,比如当我点击搜索字段时,我需要在搜索字段下显示一个条件列表。根据我需要搜索的标准。如何将此列表添加到搜索字段。提前致谢。
问问题
476 次
1 回答
0
对于在 中动态显示的列表SearchField
,您必须先拖放outlet
of searchfield
,window
然后将委托连接searchfield
到,fileowners
一旦完成,然后在代码下面进行 impelement:-
在下面的代码中,它包含一个元素数组,如果您在搜索字段中输入任何长度大于 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 回答