2014-07-21 104 views
3

我開發一個應用程序適用於MacOS X在Xcode5如何自動完成NSComboBox

我希望當用戶輸入或刪除文字,例如自動完成輸入文本框上的選項,如果用戶鍵入「我」,然後選擇墨西哥顯示在選項列表,到目前爲止,這是我的代碼:

@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,但什麼也沒有發生,什麼是正確的代碼顯示我的自動完成

回答

8

G s野兔一個例子,我希望這可以幫助:

#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 
+0

我工作得很好,我改進了用於激活按鍵,選項卡或背面選項卡驗證的代碼,謝謝! – Jesus

相關問題