PDA

View Full Version : basic secondary NIB question[SOLVED]




cromestant
Jan 29, 2009, 10:43 AM
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

@interface NewBill : NSWindowController {

}

//
// 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 :

//
// 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 :

IBOutlet NSTextField *numControl;


[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?



HiRez
Jan 29, 2009, 01:42 PM
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?

cromestant
Jan 29, 2009, 01:46 PM
i m sorry i did not repost it all, here is the code with the outlet beeing used


//
// 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

cromestant
Jan 30, 2009, 05:11 PM
moved it to the awakeFromNib file and it works like a charm@

HiRez
Jan 30, 2009, 07:53 PM
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 (http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/Documents/Tasks/FAQ.html#//apple_ref/doc/uid/20000954-1081016), but it's not entirely clear to me from that what the calling order is.

cromestant
Jan 30, 2009, 08:45 PM
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.

cromestant
Jan 31, 2009, 12:03 PM
ok, this is the link that i got that made me move to awakeFromNib method instead. ( it makes sense actually).

Click me :D! (http://www.cocoadev.com/index.pl?AwakeFromNib)

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.