【问题标题】:How to autocomplete values on NSComboBox如何在 NSComboBox 上自动完成值
【发布时间】:2014-07-21 22:44:06
【问题描述】:

我正在 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 的委托和数据源,但没有任何反应,显示自动完成的正确代码是什么

【问题讨论】:

    标签: macos autocomplete nscombobox


    【解决方案1】:

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

    #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
    

    【讨论】:

    • 我工作得很好,我改进了仅在按键、标签或后退标签上激活验证的代码,谢谢!!!
    【解决方案2】:

    确保以编程方式将 NSComboBox 的“完成”实例属性设置为 YES,或者您也可以在情节提要编辑器中找到名为“内容自动完成”的复选框。

    https://developer.apple.com/documentation/appkit/nscombobox/1436749-completes?language=objc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-23
      • 2020-10-14
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多