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


    2012年12月23日 11:39:32 Dean_jiao 阅读数:2471

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiaoguifeng/article/details/8393006

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

    pastedGraphic.png

    1.      首先调用NSComboBox的父类NSTextField的delegate方法,实现实时输入监测。其中比较关键的方法是-(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];

    }

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

    NSComboBox 使用方法

    在XCode里面

    功能:

    1.给combobox填充值 

    2.选中combobox后变化 

    3.删除cobobox里面的值

    4.指定combobox里面显示的值

    m文件

    #import "Your AppDelegate.h"

    @implementation QXSAppDelegate

    @synthesize m_combobox;

    @synthesize m_LB_Show;

    @synthesize  m_BT_1;

    - (void)dealloc

    {

        [super dealloc];

    }

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification

    {

        // Insert code here to initialize your application

        m_LB_Show.stringValue = @"";

        [self OnBT_FullValues:m_BT_1];

        [self OnComboboxChanged:m_combobox];

        

    }

    - (IBAction)OnBT_FullValues:(id)sender

    {

        id value1;

        NSString *value_str1;

        

        // Delete all item

        [m_combobox removeAllItems];

        

        // Add items to ComboBox

        value_str1 = @"Jan";

        value1=value_str1;

        [m_combobox addItemWithObjectValue:(value1)];

        [m_combobox addItemWithObjectValue:(@"Fre")];

        [m_combobox addItemWithObjectValue:(@"March")];

         [m_combobox addItemWithObjectValue:(@"April")];

         [m_combobox addItemWithObjectValue:(@"May")];

         [m_combobox addItemWithObjectValue:(@"June")];

         [m_combobox addItemWithObjectValue:(@"July")];

         [m_combobox addItemWithObjectValue:(@"August")];

         [m_combobox addItemWithObjectValue:(@"September")];

         [m_combobox addItemWithObjectValue:(@"Octorber")];

        

        // Set show Item

        [m_combobox selectItemAtIndex:0];

    }

    - (IBAction)OnBT_FullValues1:(id)sender

    {

        id value1;

        NSString *value_str1;

        

        // Delete all item

        [m_combobox removeAllItems];

        

        // Add items to ComboBox

        value_str1 = @"Monday";

        value1=value_str1;

        [m_combobox addItemWithObjectValue:(value1)];

        [m_combobox addItemWithObjectValue:(@"Tuesday")];

        [m_combobox addItemWithObjectValue:(@"Wednesday")];

        [m_combobox addItemWithObjectValue:(@"Thurday")];

        [m_combobox addItemWithObjectValue:(@"Friday")];

        [m_combobox addItemWithObjectValue:(@"Saturday")];

        [m_combobox addItemWithObjectValue:(@"Sunday")];

        

        // Set show Item

        [m_combobox selectItemAtIndex:0];

    }

    - (IBAction)OnBTAnother:(id)sender

    {

        NSInteger int_count = 0;

        [m_combobox addItemWithObjectValue:(@"1")];

        [m_combobox addItemWithObjectValue:(@"2")];

        [m_combobox addItemWithObjectValue:(@"3")];

        [m_combobox addItemWithObjectValue:(@"4")];

        [m_combobox addItemWithObjectValue:(@"5")];

        [m_combobox addItemWithObjectValue:(@"6")];

    }

    - (IBAction)OnBTClear:(id)sender

    {

        // Delete all item

        [m_combobox removeAllItems];

        m_combobox.stringValue = @"";

    }

    - (IBAction)OnComboboxChanged:(id)sender

    {

        NSInteger index_for_combox = [m_combobox indexOfSelectedItem];

        NSString *m_text_combobox;

        m_text_combobox = [m_combobox itemObjectValueAtIndex:index_for_combox];

        

        if ([m_text_combobox isEqualToString:@""]==YES)

        {

            m_LB_Show.stringValue = @"";

        }

        else

        {

            m_LB_Show.stringValue = m_text_combobox;

        }

    }

    @end

    h文件

    #import <Cocoa/Cocoa.h>

    @interface QXSAppDelegate : NSObject <NSApplicationDelegate>

    @property (assign) IBOutlet NSWindow *window;

    @property (assign) IBOutlet NSTextField *m_LB_Show;

    @property (assign) IBOutlet NSButton *m_BT_1;

    @property (assign) IBOutlet NSComboBox *m_combobox;

    @end

    完毕!

    --------------------- 

    原文:https://blog.csdn.net/u013317006/article/details/27104121

  • 相关阅读:
    JMeter递增加压总结
    JMeter跨线程传参总结
    JMeter+Grafana+Influxdb可视化性能监控平台搭建总结
    MONyog入门总结
    MongoDB导出/导入操作
    测试工作中用到的MongoDB命令
    Go单元测试与报告
    Postwoman教程
    测试工作中用到的Redis命令
    redis-dump教程
  • 原文地址:https://www.cnblogs.com/sundaymac/p/10337195.html
Copyright © 2020-2023  润新知