我正在Xcode5中为MacOS X开发应用程序
当用户键入或删除文本时,我想在文本字段上键入自动填充选项,例如,如果用户键入" Me"然后墨西哥选项显示在选项列表中,到目前为止这是我的代码:
@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的委托和数据源,但没有任何反应,显示我的自动完成的正确代码是什么
答案 0 :(得分:8)
分享一个例子我希望这会有所帮助:
#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
答案 1 :(得分:0)
确保以编程方式将NSComboBox的“ completes”实例属性设置为YES,或者在情节提要编辑器中找到一个名为“ Content AutoCompletes”的复选框。
https://developer.apple.com/documentation/appkit/nscombobox/1436749-completes?language=objc