gpt4 book ai didi

objective-c - 如何动态添加列表到搜索字段?

转载 作者:行者123 更新时间:2023-12-03 16:46:21 26 4
gpt4 key购买 nike

我想实现一个功能,例如当我单击搜索字段时,我需要在搜索字段下显示条件列表。根据我需要搜索的标准。我如何将此列表添加到搜索字段。提前致谢。

最佳答案

要在 SearchField 中动态显示列表,您必须将 searchfieldoutlet 拖放到 window 中> 首先,然后将 searchfield 的委托(delegate)连接到 fileowners 完成后,然后执行以下代码:-

在下面的代码中,它包含一个元素数组,如果您在搜索字段中输入任何长度大于3的单词,那么它将根据数组中的匹配单词显示结果列表.

-(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
}

关于objective-c - 如何动态添加列表到搜索字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20683946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com