Tons of fun w/NSComboBox

  • I figured out how to change the contents of an NSComboBox dynamically, and
    it's working great! I'm just loading it with the contents of an
    NSMutableArray, using

        [myComboBox addItemsWithObjectValues:myMutableArray];

    I've also set my combo box to auto-complete

        [myComboBox setCompletes:YES];

    The only problem is that if I type something in and it's not the correct
    case, it doesn't auto-complete, and will end up adding another item to the
    combo box.

    How would I make the auto-complete case-insensitive?
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Looks like you would have to override the NSComboBoxCell method: -
    (NSString *)completedString:(NSString *)substring.

    AppKiDo is great for this kind of thing.

    On Tuesday, August 12, 2003, at 09:32  PM, Mike Brinkman wrote:

    > I figured out how to change the contents of an NSComboBox dynamically,
    > and
    > it's working great! I'm just loading it with the contents of an
    > NSMutableArray, using
    >
    > [myComboBox addItemsWithObjectValues:myMutableArray];
    >
    > I've also set my combo box to auto-complete
    >
    > [myComboBox setCompletes:YES];
    >
    > The only problem is that if I type something in and it's not the
    > correct
    > case, it doesn't auto-complete, and will end up adding another item to
    > the
    > combo box.
    >
    > How would I make the auto-complete case-insensitive?
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • I just downloaded AppKiDo, it's pretty cool. A bit easier than using PB or
    Safari to browse. Thanks for the tip!

    Still, I'm not sure how this all works.

    I read the "Combo Boxes" programming topic, and it said "Every method in
    NSComboBoxCell has a cover in NSComboBox. (A cover is a method of the same
    name that calls the original method.)"

    If I look up NSComboBox, I don't find the method:

        (NSString *)completedString:(NSString *)substring

    If I override (NSString *)completedString:(NSString *)substring, does it
    apply to every NSComboBox on my app, or does it apply only to a particular
    NSComboBox? Let's say I've got an NSComboBox called myBox. How do I use
    completedString with that? Would it be something like:

    [myBox completedString:[myBox stringValue]] ?

    I simply want the compare for the auto-completion just to ignore case. If I
    override the (NSString *)completedString:(NSString *)substring method, how
    do I insure that I still get the same functionality?

    This seems like a lot of work for something pretty trivial. Especially when
    so many Foundation classes have some sort of easy method for doing what I'm
    asking for, like NSString's:
        -(NSComparisonResult)caseInsensitiveCompare:(NSString *)aString

    Any further light shed on this subject would be greatly appreciated.

    On 8/12/03 11:03 PM, "tony cate" <tonycate...> wrote:

    > Looks like you would have to override the NSComboBoxCell method: -
    > (NSString *)completedString:(NSString *)substring.
    >
    > AppKiDo is great for this kind of thing.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • > If I override (NSString *)completedString:(NSString *)substring, does it
    > apply to every NSComboBox on my app, or does it apply only to a particular
    > NSComboBox?

    The latter.  You should subclass NSComboBoxCell and write your own
    implementation of completedString.  In awakeFromNib, you should set the cell
    of the combo cox to be an instance of your subclass.  Your implementation
    will work only for instances of your subclass, so it will only work for
    combo boxes where you have explicitly set the cell to be your cell.

    > This seems like a lot of work for something pretty trivial.

    Loop through the object values.  Stop when you hit one whose first n
    characters match what the user has typed.  If you want the comparison to be
    case insensitive, convert them both to lowercase or uppercase first.  Return
    the matching value if there is one, nil otherwise.

    Its even simpler if your list consists of items whose words are all
    capitalized.  Then you do:

    - (NSString *)completedString:(NSString *)substring
    {
    return [super completedString:[substring capitalizedString]];
    }

    The same kind of idea could be applied to a list that is totally lowercase
    or uppercase.

    Doesn't seem like that much work.

    Jonathan
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Here is what I tried:

    #import <AppKit/AppKit.h>

    @interface BetterComboBoxCell : NSComboBoxCell {

    }

    @end

    #import "BetterComboBoxCell.h"

    @implementation BetterComboBoxCell

    - (NSString *)completedString:(NSString *)substring
    {
        return [super completedString:[substring capitalizedString]];
    }

    - (void)dealloc
    {
        [super dealloc];
    }

    @end

    And then in my Document.h file declare:

    BetterComboBoxCell *myComboBoxCell;

    Then in my Document.m file declare:

    - (void)awakeFromNib
    {
        ...
        [myComboBox setCell:myComboBoxCell];
        ...
    }

    It compiles, but when I run it, my combo boxes disappear. What am I doing
    wrong?

    On 8/13/03 3:52 PM, "Jonathan E. Jackel" <jonathan...> wrote:

    >
    >> If I override (NSString *)completedString:(NSString *)substring, does it
    >> apply to every NSComboBox on my app, or does it apply only to a particular
    >> NSComboBox?
    >
    > The latter.  You should subclass NSComboBoxCell and write your own
    > implementation of completedString.  In awakeFromNib, you should set the cell
    > of the combo cox to be an instance of your subclass.  Your implementation
    > will work only for instances of your subclass, so it will only work for
    > combo boxes where you have explicitly set the cell to be your cell.
    >
    >> This seems like a lot of work for something pretty trivial.
    >
    > Loop through the object values.  Stop when you hit one whose first n
    > characters match what the user has typed.  If you want the comparison to be
    > case insensitive, convert them both to lowercase or uppercase first.  Return
    > the matching value if there is one, nil otherwise.
    >
    > Its even simpler if your list consists of items whose words are all
    > capitalized.  Then you do:
    >
    > - (NSString *)completedString:(NSString *)substring
    > {
    > return [super completedString:[substring capitalizedString]];
    > }
    >
    > The same kind of idea could be applied to a list that is totally lowercase
    > or uppercase.
    >
    > Doesn't seem like that much work.
    >
    > Jonathan
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • You're combo boxes disappear because you've set their cell to nil.  You need
    to create an instance of your cell, not merely declare a variable for it.
    You say:

    >
    > And then in my Document.h file declare:
    >
    >
    > BetterComboBoxCell *myComboBoxCell;

    That's fine, although probably unnecessary.

    >
    >
    > Then in my Document.m file declare:
    >
    > - (void)awakeFromNib
    > {
    > ...
    > [myComboBox setCell:myComboBoxCell];
    > ...
    > }

    I would do it this way:

    - (void)awakeFromNib
    {
        ...
        [myComboBox setCell:[[[BetterComboBoxCell alloc] init] autorelease]];
        ...
    }

    I'm assuming that you don't really need variable that refers to the cell;
    you just need some way of sticking the cell in your combo box.  The combo
    box will retain its cell, so you can autorelease it when it is created.

    Jonathan
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Wednesday, August 13, 2003, at 12:52  PM, Jonathan E. Jackel wrote:

    >> If I override (NSString *)completedString:(NSString *)substring, does
    >> it
    >> apply to every NSComboBox on my app, or does it apply only to a
    >> particular
    >> NSComboBox?
    >
    > The latter.  You should subclass NSComboBoxCell and write your own
    > implementation of completedString.  In awakeFromNib, you should set
    > the cell
    > of the combo cox to be an instance of your subclass.  Your
    > implementation
    > will work only for instances of your subclass, so it will only work for
    > combo boxes where you have explicitly set the cell to be your cell.
    >

    In Cocoa, you very rarely need to subclass objects in the AppKit.
    According to the documentation for completedString: in NSComboBoxCell,
    you can do it this way:

    The default implementation of this method first checks whether the
    combo box uses a data source and whether the data source responds to
    comboBox:completedString: or comboBoxCell:completedString:

    Also take a look at the documentation for NSComboBoxDataSource, this
    explains the use of the completedString: method used by the data source.

    Don
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month august 2003 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Go to today