basic secondary NIB question[SOLVED]

Discussion in 'Mac Programming' started by cromestant, Jan 29, 2009.

  1. macrumors member

    Joined:
    Apr 27, 2006
    #1
    Hello, I'm currently learning on my own how to program in cocoa, and its quite fun, but i ve run into a few little problems, and I would love some help.


    Ok, so first of all , i have My main window nib file with a button that uppon beeing pressed lauches a secondary window. This secondary window is an instance of NSWindowController, it launches properly with this code

    Code:
    @interface NewBill : NSWindowController {
    	
    }
    Code:
    //
    //  NewBill.m
    //  Facturacion
    //
    //  Created by Charles Romestant on 1/28/09.
    //  Copyright 2009 __MyCompanyName__. All rights reserved.
    //
    
    #import "NewBill.h"
    
    
    @implementation NewBill
    
    - (id) init {
    	
    	if ( ! (self = [super initWithWindowNibName: @"nibFactura"]) ) {
    		NSLog(@"init failed in TheWindowController");
    		return nil;
    	} // end if
    	NSLog(@"init OK in TheWindowController");
    	return self;
    } // end init
    
    - (void)windowDidLoad {
    	NSLog(@"TheWindowPanel did load");
    	
    } // end windowDidLoad
    
    
    @end
    
    and from my mainAppController :
    Code:
    //
    //  AppController.m
    //  Facturacion
    //
    //  Created by Charles Romestant on 1/28/09.
    //  Copyright 2009 __MyCompanyName__. All rights reserved.
    //
    
    #import "AppController.h"
    #import "NewBill.h"
    
    @implementation AppController
    
    -(IBAction) showTheWindow:(id)pId;
    {
    	NSLog(@"Hi from TheAppController showTheWindow");
    	if (! BillWindow ) {
    		BillWindow	= [[NewBill alloc] init];
    	} // end if
    	
    	[BillWindow showWindow:self];
    }
    @end
    
    Now, on this secondary window i decided to add a textfield so that i can "fill" it when the window loads ( so that later on i can fill it with precalculated values from a sqlite file).
    so I added the outlet on the .h file, added an NSObject from the interface builder, set its type to the NWwindowController class ( NewBill)

    set the link in interface builder and added this
    in the header :
    Code:
    IBOutlet NSTextField *numControl;
    
    Code:
    [numControl setStringValue:@"Loaded from NewBill.m"];
    
    So the compiel works, no errors, no warnings, my button still spawns the new window, but no text is in the NSTextField.

    what could be going on?
     
  2. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    Where is that line of code that sets the string value of the text field? What happens when you call that in windowDidLoad:? And what's it say if you check the outlet for nil before calling it?
     
  3. thread starter macrumors member

    Joined:
    Apr 27, 2006
    #3
    i m sorry i did not repost it all, here is the code with the outlet beeing used

    Code:
    //
    //  NewBill.m
    //  Facturacion
    //
    //  Created by Charles Romestant on 1/28/09.
    //  Copyright 2009 __MyCompanyName__. All rights reserved.
    //
    
    #import "NewBill.h"
    
    
    @implementation NewBill
    
    - (id) init {
    	
    	if ( ! (self = [super initWithWindowNibName: @"nibFactura"]) ) {
    		NSLog(@"init failed in TheWindowController");
    		return nil;
    	} // end if
    	NSLog(@"init OK in TheWindowController");
    	return self;
    } // end init
    
    - (void)windowDidLoad {
    	NSLog(@"TheWindowPanel did load");
    	[numControl setStringValue:@"Loaded from NewBill.m"];
    } // end windowDidLoad
    
    
    @end
    
     
  4. thread starter macrumors member

    Joined:
    Apr 27, 2006
    #4
    moved it to the awakeFromNib file and it works like a charm@
     
  5. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #5
    That's cool that it works, but I'm still confused as to why it's not working in windowDidLoad: as well. I *thought* that outlets were guaranteed to be valid when that gets called, in other words after awakeFromNib:, but apparently not. So I guess using awakeFromNib: makes sense, because we do know that in that method at least, all connected outlets are guaranteed to be valid. Then there's windowControllerDidLoadNib: as well. There's a little about that stuff here, but it's not entirely clear to me from that what the calling order is.
     
  6. thread starter macrumors member

    Joined:
    Apr 27, 2006
    #6
    I'm postign from my phone s I don't have the link at hand but aparently outlets might not have been initializd on that call. Plus it only gets called once so it's not what I need awake from nib is what I need i'll post links Tomorow.
     
  7. thread starter macrumors member

    Joined:
    Apr 27, 2006
    #7
    ok, this is the link that i got that made me move to awakeFromNib method instead. ( it makes sense actually).

    Click me :D!

    Also, what happens, is that awakeFromNib gets called on every spawn of the window, whereas the other only gets called on the first time, so for instance if you want to calculate a value that is needed on your new window ( for instance to pre-fill a value with an id or such, then you d only get it on the first time, and not on all other instances.
     

Share This Page