I must be missing something obvious here.
I'm trying to load a few items in an NSPopUpButton when the app starts. I added the following code in theinit
method:
NSArray *listOfProfiles = [[NSArray alloc] initWithObjects:@"My Item 0", @"My Item 1", nil];
[profileListPopUp addItemsWithTitles:listOfProfiles];
NSLog(@"item 0 %@", [profileListPopUp itemTitleAtIndex:0]);
NSLog(@"item 1 %@", [profileListPopUp itemTitleAtIndex:1]);
And the output I get is:
2011-09-24 08:27:39.147 MyApp[3794:707] item 0 (null)
2011-09-24 08:27:39.148 MyApp[3794:707] item 1 (null)
However, if I put the code in another method that's called when pressing a different button, it works fine.
It seems that theinit
method is called before the NSPopUpButton is created but, in that case, I'd expect it to crash when referencing it.
Where should I put my code?
Bonus question: how do I get rid of the default values (other than calling Remov开发者_StackOverflow中文版eAll
on the control) that are loaded in the NSPopUpButton
: 'Item 1', 'Item2' and 'Item 3'.
If you've not heard of the -awakeFromNib
or -windowDidLoadNib
methods, now would be a great time to read about them. My guess is your profileListPopUp pointer is at the time you're trying to add items to it because it's not been fully loaded from a nib yet.
精彩评论