开发者

How to set initial values of NSPopUpButton at startup in Cocoa

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 the init 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 the init 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 -windowDidLoadNibmethods, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜