Bruno Ferreira
Bruno Ferreira

Reputation: 952

How to properly use NSPopUpButton

I have a NSPopUpButton that I want to use to select the text encoding for opening a file.

I already have some ideas how to implement this, but as I'm starting to learn Objective-C and Cocoa I'm almost sure that there is a better way to accomplish what i want.

I need to have a NSString with the name of the encoding and an associated NSStringEncoding value.

I have thought creating a class representing an encoding (name and value) and have a NSArray with objects of this type and then populate the NSPopUpButton with the contents of the array, but I think that there should be a better way.

I'm not very familiar with the NSDictionary class but I suspect that should make things easier.

Can someone give me a hint on this?

Upvotes: 3

Views: 4853

Answers (2)

Motti Shneor
Motti Shneor

Reputation: 2194

You could tackle this much simpler. The NSPopupButton has an NSMenu object with all the NSMenuItems you let the user choose from.

The basic API of NSPopupButton only allows adding simple "itemWithTitle" but not otherwise customize and modify the menu-items created.

- (void)addItemWithTitle:(NSString *)title;

But as soon as you called this, you can use the following NSPopupButton API:

@property (nullable, readonly, strong) NSMenuItem *lastItem;

to get hold of the internal NSMenuItem object created with your specified title, and them customize it directly.

Here - you can use the fact that NSMenuItem can be assigned a numeric 'tag', and tag the menu item with your actual encoding (which is an integer).

Then, handling the user's choice is straightforward -- the NSPopUpButton action called, gives you always the 'sender' (which object sent the action) that will be the actual NSMenuItem selected by the user. You can simply ask it for its tag - and there you have it. The text encoding the user chose.

So... the code will look like this:

NSPopUpButton *pop = [[NSPopUpButton alloc] init];
// add a new menu item for ASCII
[pop addItemWithTitle:@"ASCII Encoding";
NSMenuItem *popUpItem = pop.lastItem;
[popUpItem setTag: NSASCIIStringEncoding]

And then, in your menu action:

  • (IBAction)encodingMenuAction:(id)sender { NSStringEncoding selectedEncoding = (NSStringEncoding)[sender tag]; // do whatever you want to do with the encoding }

or, at any time you want to know what is the currently selected encoding:

   NSStringEncoding selectedEncoding = (NSStringEncoding)[[pop selectedItem ] tag];

Upvotes: 0

Frank
Frank

Reputation: 2413

Create the dictionary with encodings as value and the names for the NSPopUpButton as keys

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
NSNumber numberWithLong:NSASCIIStringEncoding], @"ASCII", 
[NSNumber numberWithLong:NSUnicodeStringEncoding], @"Unicode", nil];

Then add them to the NSPopUpButton with

[myPopUpButton addItemsWithTitles:[dict allKeys]]

Then get the encoding the user selected with

[dict objectForKey:[myPopUpButton titleOfSelectedItem]]

Note: you will need to wrap the string encoding enum in a object, like NSValue or NSNumber.

Upvotes: 6

Related Questions