Why 'window title' is null?

Discussion in 'Mac Programming' started by youngplayer, Jul 4, 2008.

  1. youngplayer macrumors member

    Joined:
    May 16, 2008
    Location:
    Shanghai,China
    #1
    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?
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    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.
     
  3. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3
    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.
     

Share This Page