NSComboBox输入字符时自动打开下拉菜单并匹配

 

     NSComboBox,此功能实现的效果图如下图所示:

1.      首先调用NSComboBox的父类NSTextFielddelegate方法,实现实时输入监测。其中比较关键的方法是-(void)controlTextDidChange:(NSNotification*)notification,这个方法可以实现输入内容的实时监测。

         -(void)controlTextDidChange:(NSNotification*)notification

{

   id object = [notification object];

   [object setComplete:YES];//这个函数可以实现自动匹配功能

}

2.      可以看到,在NSComboBox控件的右边有一个标有下三角的按钮,这个按钮在鼠标点击后才弹出下拉菜单来,但是我们在输入的时候没有实现鼠标事件,所以下来菜单无法弹出,因此需要模拟鼠标事件-(void)mouseDown:(NSEvent*)theEvent,但是这个按钮是NSComboBoxCell中的私有变量,所以我们重新创建一个类,这个类继承NSComboBoxCell,这样我们就可以应用它的私有变量了。

继承NSComboBox类:

@interface ComboBoxCell : NSComboBoxCell

{

}

 

- (void)popUpList;

- (void)closePopUpWindow;

- (BOOL)isPopUpWindowVisible;

@end

 

@implementation ComboBoxCell

- (void)popUpList

{

if ([self isPopUpWindowVisible]) 

{

return;

}

else

{

[_buttonCell performClick:nil];//模拟鼠标事件

}

}

- (void)closePopUpWindow

{

if ([self isPopUpWindowVisible])

{

[_popUp close];

}

}

- (BOOL)isPopUpWindowVisible

{

return [_popUp isVisible];

}

@end

3.  实现NSComboBox类的Datasource方法:

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

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

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

这是Datasource方法,我们需要重写此方法。下面是三个方法的实现,方法中的结构体可以根据用户的需要自定义。

 

- (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];

}

    做到这里,这个功能就基本实现了,本文中的代码来自正在开发的工程,读者只需替换响应的结构体,即可实现功能。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值