NSComboBox抵栈,此功能實(shí)現(xiàn)的效果圖如下圖所示:
1.? ? ? 首先調(diào)用NSComboBox的父類NSTextField的delegate方法,實(shí)現(xiàn)實(shí)時(shí)輸入監(jiān)測(cè)庞呕。其中比較關(guān)鍵的方法是-(void)controlTextDidChange:(NSNotification*)notification,這個(gè)方法可以實(shí)現(xiàn)輸入內(nèi)容的實(shí)時(shí)監(jiān)測(cè)巢音。
-(void)controlTextDidChange:(NSNotification*)notification
{
id object = [notification object];
[object setComplete:YES];//這個(gè)函數(shù)可以實(shí)現(xiàn)自動(dòng)匹配功能
}
2. ?可以看到鳍贾,在NSComboBox控件的右邊有一個(gè)標(biāo)有下三角的按鈕,這個(gè)按鈕在鼠標(biāo)點(diǎn)擊后才彈出下拉菜單來般贼,但是我們?cè)谳斎氲臅r(shí)候沒有實(shí)現(xiàn)鼠標(biāo)事件愧哟,所以下來菜單無法彈出奥吩,因此需要模擬鼠標(biāo)事件-(void)mouseDown:(NSEvent*)theEvent,但是這個(gè)按鈕是NSComboBoxCell中的私有變量蕊梧,所以我們重新創(chuàng)建一個(gè)類霞赫,這個(gè)類繼承NSComboBoxCell,這樣我們就可以應(yīng)用它的私有變量了肥矢。
繼承NSComboBox類:
@interface ComboBoxCell : NSComboBoxCell
{
}
- (void)popUpList;
- (void)closePopUpWindow;
- (BOOL)isPopUpWindowVisible;
@end
@implementation ComboBoxCell
- (void)popUpList
{
if ([self isPopUpWindowVisible])
{
return;
}
else
{
[_buttonCell performClick:nil];//模擬鼠標(biāo)事件
}
}
- (void)closePopUpWindow
{
if ([self isPopUpWindowVisible])
{
[_popUp close];
}
}
- (BOOL)isPopUpWindowVisible
{
return [_popUp isVisible];
}
@end
3.? 實(shí)現(xiàn)NSComboBox類的Datasource方法:
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox;
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index;
- (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string;
這是Datasource方法端衰,我們需要重寫此方法。下面是三個(gè)方法的實(shí)現(xiàn)甘改,方法中的結(jié)構(gòu)體可以根據(jù)用戶的需要自定義旅东。
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox
{
NSInteger row = 0;
if (currentBoxIndex_ == -1)
{
return row;
}
DBBoxManager *currentBox = [[DBManager defaultDBManager] boxAtIndex:currentBoxIndex_];
if ([vendorControl_ isEqual:aComboBox])
{
row = [currentBox userDefineVenderInfos].count;
}
else if ([categoryControl_ isEqual:aComboBox])
{
row = [currentBox userDefineCategoryInfos].count;
}
else if ([paymentControl_ isEqual:aComboBox])
{
row = [currentBox userDefinePaymentInfos].count;
}
else if ([purposeControl_ isEqual:aComboBox])
{
row = [currentBox userDefinePurposeInfos].count;
}
else if ([categorySelectBtn_ isEqual:aComboBox])
{
row = [currentBox userDefineCategoryInfos].count;
}
else if([vendorSelectBtn_ isEqual:aComboBox])
{
row = [currentBox userDefineVenderInfos].count;
}
row = row + 1;
return row;
}
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index
{
NSString *content = nil;
if (currentBoxIndex_ == -1)
{
return content;
}
NSMutableArray *array = nil;
DBBoxManager *currentBox = [[DBManager defaultDBManager] boxAtIndex:currentBoxIndex_];
if ([vendorControl_ isEqual:aComboBox])
{
array = [currentBox userDefineVenderInfos];
}
else if ([categoryControl_ isEqual:aComboBox])
{
array = [currentBox userDefineCategoryInfos];
}
else if ([paymentControl_ isEqual:aComboBox])
{
array = [currentBox userDefinePaymentInfos];
}
else if ([purposeControl_ isEqual:aComboBox])
{
array = [currentBox userDefinePurposeInfos];
}
else if ([categorySelectBtn_ isEqual:aComboBox])
{
array = [currentBox userDefineCategoryInfos];
}
else if ([vendorSelectBtn_ isEqual:aComboBox])
{
array = [currentBox userDefineVenderInfos];
}
if (index == 0)
{
content = @"";
}
else
{
NSDictionary *dic = [array objectAtIndex:index - 1];
content = (NSString *)[dicobjectForKey:kUserDefinedBoxValue];
}
return content;
}
- (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string
{
DBBoxManager *currentBox = [[DBManager defaultDBManager] boxAtIndex:currentBoxIndex_];
NSMutableArray *comboxList = nil;
if ([aComboBox isEqual:vendorControl_])
{
comboxList = [currentBox userDefineVenderInfos];
}
else if([aComboBox isEqual:categoryControl_])
{
comboxList = [currentBox userDefineCategoryInfos];
}
else if([aComboBox isEqual:paymentControl_])
{
comboxList = [currentBox userDefinePaymentInfos];
}
else if([aComboBox isEqual:purposeControl_])
{
comboxList = [currentBox userDefinePurposeInfos];
}
NSMutableArray *newList = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
[newList addObject:@""];
for (int i = 0; i < [comboxList count]; i++)
{
NSString *name = [[comboxList objectAtIndex:i] objectForKey:kUserDefinedBoxValue];
[newList addObject:name];
}
return [newList indexOfObject:string];
}
做到這里,這個(gè)功能就基本實(shí)現(xiàn)了十艾,本文中的代碼來自正在開發(fā)的工程抵代,讀者只需替換響應(yīng)的結(jié)構(gòu)體,即可實(shí)現(xiàn)功能忘嫉。