gpt4 book ai didi

macos - 如何在 NSComboBox 上自动完成值

转载 作者:行者123 更新时间:2023-12-01 09:25:42 27 4
gpt4 key购买 nike

我正在 Xcode5 中为 MacOS X 开发应用程序

我想在用户键入或删除文本时自动完成在文本字段上键入的选项,例如,如果用户键入“我”,则墨西哥选项会显示在选项列表中,到目前为止,这是我的代码:

@interface ComboNSObject()<NSComboBoxCellDataSource, NSComboBoxDataSource, NSComboBoxDelegate>{
NSArray *datos;
}

@property (weak) IBOutlet NSComboBox *myCombo;

@end


@implementation ComboNSObject

-(void)awakeFromNib{
datos = [[NSArray alloc]initWithObjects:@"Mexico",@"Guatemala",@"USA",@"Chile",@"Argentina", nil];

[_myCombo addItemsWithObjectValues:datos];

}


- (NSString *)comboBox:(NSComboBox *)comboBox completedString:(NSString *)partialString
{
for (NSString *dataString in datos) {
NSLog(@"encontrado: %@", [dataString commonPrefixWithString:partialString options:NSCaseInsensitiveSearch]);
}
return @"";
}


@end

我已经在我的 NSObjectController 上设置了 _myCombo 及其 NSComboBoxCell 的委托(delegate)和数据源,但没有任何反应,显示自动完成的正确代码是什么

最佳答案

分享一个例子,希望对你有帮助:

#import "AppDelegate.h"
@interface AppDelegate() <NSComboBoxDataSource, NSComboBoxDelegate, NSControlTextEditingDelegate>{

NSMutableArray *itemsCombo;
NSMutableArray *filteredItemsCombo;

}

@property (weak) IBOutlet NSComboBox *myComboBox;

@end


@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

itemsCombo = [NSMutableArray arrayWithObjects:@"Dog",@"Cat",@"Worm",@"Wolf",@"Werewolf",@"Lion",@"Beast", nil];
filteredItemsCombo = [[NSMutableArray alloc]initWithArray:itemsCombo];


_myComboBox.usesDataSource = YES;
_myComboBox.completes = YES;
_myComboBox.dataSource = self;
_myComboBox.delegate = self;
}


- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox{

return filteredItemsCombo.count;
}


- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index{

return filteredItemsCombo[index];
}


- (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string{

return [filteredItemsCombo indexOfObject:string];
}


-(void)comboBoxWillPopUp:(NSNotification *)notification{
[self resultsInComboForString:((NSComboBox *)[notification object]).stringValue];
}


-(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector{

NSComboBox *comboBox = (NSComboBox *) control;

if (comboBox == _myComboBox && (commandSelector == @selector(insertNewline:) ||
commandSelector == @selector(insertBacktab:) ||
commandSelector == @selector(insertTab:))){
if ([self resultsInComboForString:comboBox.stringValue].count == 0 ||
filteredItemsCombo.count == itemsCombo.count) {
comboBox.stringValue = itemsCombo[0];
}

}

return NO;
}


- (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)string {


NSArray *currentList = [NSArray arrayWithArray:itemsCombo];

NSEnumerator *theEnum = [currentList objectEnumerator];
id eachString;
NSInteger maxLength = 0;
NSString *bestMatch = @"";
while (nil != (eachString = [theEnum nextObject]) )
{
NSString *commonPrefix = [eachString
commonPrefixWithString:string options:NSCaseInsensitiveSearch];
if (commonPrefix.length >= string.length &&
commonPrefix.length > maxLength)
{
maxLength = commonPrefix.length;
bestMatch = eachString;

break;
}
}

[self resultsInComboForString:string];

return bestMatch;
}

-(NSArray *)resultsInComboForString:(NSString *)string{
[filteredItemsCombo removeAllObjects];

if (string.length == 0 || [string isEqualToString:@""] || [string isEqualToString:@" "]) {
[filteredItemsCombo addObjectsFromArray:itemsCombo];
}
else{
for (int i = 0; i < itemsCombo.count; i++) {

NSRange searchName = [itemsCombo[i] rangeOfString:string options:NSCaseInsensitiveSearch];
if (searchName.location != NSNotFound) { [filteredItemsCombo addObject:itemsCombo[i]]; }

}
}


[_myComboBox reloadData];

return filteredItemsCombo;
}

@end

关于macos - 如何在 NSComboBox 上自动完成值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876083/

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