Initializing a ComboBox

Discussion in 'Mac Programming' started by klkittel, Nov 18, 2011.

  1. klkittel, Nov 18, 2011
    Last edited by a moderator: Nov 19, 2011

    klkittel macrumors newbie

    Joined:
    Nov 10, 2011
    #1
    Hello,

    I followed the reference guide to NSComboBoxes, but my example ComboBox is empty, here is my code extracted from an NSWindowController:

    Code:
    - (void)windowDidLoad
    {
    	NSLog(@"New Phrases Window did load\n");
    	
    	// Load Data into Combo Boxes
    	
    	[actionField addItemWithObjectValue:@"1"];
    	[actionField addItemWithObjectValue:@"2"];
    	[actionField addItemWithObjectValue:@"3"];	
    	[actionField setStringValue:@"Red"];
    	
    }
    The message is printed, but the combobox is empty. What am I missing?
     
  2. klkittel thread starter macrumors newbie

    Joined:
    Nov 10, 2011
    #2
    OK, I've made a Combo Box in my main Window and the code above works. That leads me to believe that I haven't built my second window correctly or haven't done something correctly in interface builder. All my code for the combo box is the same between the two .h files and the connections were set up the same in interface builder.
     
  3. klkittel, Nov 18, 2011
    Last edited by a moderator: Nov 19, 2011

    klkittel thread starter macrumors newbie

    Joined:
    Nov 10, 2011
    #3
    Here is how I set up and display the window:

    From the main window:
    Code:
    - (IBAction)newPhraseWindow:(id)sender
    {
        winController = [[newPhraseWindowController alloc] init];
        [winController showWindow:self];
    }
    
    From the WindowController:

    I moved the calls to initilize the combobox to init:
    Code:
    - (id)init
    {
        self=[super initWithWindowNibName:@"newPhraseWindow"];
        if(self)
        {
    		NSLog(@"New Phrases Window Init\n");
    		
    		// Load Data into Combo Boxes
    		
    		[actionField addItemWithObjectValue:@"1"];
    		[actionField addItemWithObjectValue:@"2"];
    		[actionField addItemWithObjectValue:@"3"];	
    		[actionField setStringValue:@"Red"];
    		
        }
        return self;
    
    The window shows up, so I can't see where any of this code is at fault, but the combo box remains blank.
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    Doing this in init is too early. In init, actionField won't yet be connected to the combo box. I assume you have connected the combo box to the actionField IBOutlet in the .xib

    Trying doing it from the awakeFromNib method.
     
  5. klkittel thread starter macrumors newbie

    Joined:
    Nov 10, 2011

Share This Page