Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

youngplayer

macrumors member
Original poster
May 16, 2008
36
0
Shanghai,China
my codes:
@interface WindowController: NSWindowController{
IBOutlet NSWindow * window;
}

@implementation WindowController

-(id) init{
if(self = [super initWithWindowNibName:mad:"MyWindow"]){
// do something
}
return self;
}

-(id)initWithTitle: (NSString *) title{
[self init];
NSLog(@"window title before set is:%@",[window title]);
[window setTitle:title];
NSLog(@"window title after set is:%@",[window title]);
return self;
}
----------------------------------------------------------------------
The log shows that the title of the window is always 'null'. The IBOutlet 'window' is binding to a NSWindow object in IB which has an initial title name. why is the title 'null' before and after set? why the setTitle doesn't work?
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
I don't think you should be setting anything UI-related from a nib in an init-style method, everything probably won't be valid at that point. You'd probably want to do that by overridingNSWindowController's windowDidLoad: method.

If you are working with a Document-based application, the window's title is automatically synchronized with the associated document's name, and setTitle: calls are ignored. In that case, you have to override windowTitleForDocumentDisplayName: to set a custom title. If you were doing this, you wouldn't explicitly have to set the title anywhere in your code, as the window manager will call your overridden method for the title at the appropriate time.
 

MrFusion

macrumors 6502a
Jun 8, 2005
613
0
West-Europe
my codes:
@interface WindowController: NSWindowController{
IBOutlet NSWindow * window;
}

@implementation WindowController

-(id) init{
if(self = [super initWithWindowNibName:mad:"MyWindow"]){
// do something
}
return self;
}

-(id)initWithTitle: (NSString *) title{
[self init];
NSLog(@"window title before set is:%@",[window title]);
[window setTitle:title];
NSLog(@"window title after set is:%@",[window title]);
return self;
}
----------------------------------------------------------------------
The log shows that the title of the window is always 'null'. The IBOutlet 'window' is binding to a NSWindow object in IB which has an initial title name. why is the title 'null' before and after set? why the setTitle doesn't work?

any changes to the interface can also go in
-(void) awakeFromNib

Init methods are for creating variables you need, which is called before any nib (interface builder files) are loaded.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.