• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: AddItemsWithTitles question...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: AddItemsWithTitles question...


  • Subject: Re: AddItemsWithTitles question...
  • From: Manfred Schwind <email@hidden>
  • Date: Fri, 25 May 2007 11:48:20 +0200

What is the best way to associate a value with a title? I was looking at the
docs for NSPopUpButton but did not see a way. I would like it so "5 Minutes"
would be "300" for 300 seconds.

See setRepresentedObject: or setTag:

[delayTimePopUp addItemsWithTitles:[NSArray arrayWithObjects:@"5 Minutes",
@"10 Minutes", @"15 Minutes", @"30 Minutes", nil]];

e.g.

NSMenuItem *item;

item = [delayTimePopUp addItemWithTitle:@"5 Minutes" action:@selector (myDelayClickedAction:) keyEquivalent:@""];
[item setRepresentedObject:[NSNumber numberWithInt:5*60]];


item = [delayTimePopUp addItemWithTitle:@"10 Minutes" action:@selector (myDelayClickedAction:) keyEquivalent:@""];
[item setRepresentedObject:[NSNumber numberWithInt:10*60]];


item = [delayTimePopUp addItemWithTitle:@"15 Minutes" action:@selector (myDelayClickedAction:) keyEquivalent:@""];
[item setRepresentedObject:[NSNumber numberWithInt:15*60]];
...


Of course, you can also use setTag to set the minute values, but I usually prefer representedObject.
A better way than this ugly copy&paste code may be to read title and value from a static array or something like that (or compose the title automatically from the values), e.g.:


static int minuteValues[] = {5, 10, 15, 30, 0}; // 0 at the end is required for the following code!
int *valuePtr = minuteValues;
int value;
while ((value = *valuePtr++) != 0) {
NSMenuItem *item = [delayTimePopUp addItemWithTitle:[NSString stringWithFormat:@"%d Minutes", value] action:@selector (myDelayClickedAction:) keyEquivalent:@""];
[item setRepresentedObject:[NSNumber numberWithInt:(value * 60)]];
}


(Directly typed into Mail, please forgive any errors ...)

Regards,
Mani
--
http://www.mani.de
iVolume - Loudness adjustment for iTunes.
LittleSecrets - The encrypted notepad.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >AddItemsWithTitles question... (From: Tom Jones <email@hidden>)

  • Prev by Date: NSColor + Bindings
  • Next by Date: NSTreeController bound selection issue
  • Previous by thread: AddItemsWithTitles question...
  • Next by thread: Re: Cocoa-dev Digest, Vol 4, Issue 496
  • Index(es):
    • Date
    • Thread