NSWindow toolbar remains nil

  • Hi, all!

    I'm having the following rather frustrating problem to which I
    haven't found a solution by searching.  In an NSPersistentDocument
    subclass, I try the following code in -windowControllerDidLoadNib: :

        //create the toolbar
        mainToolbar = [[[NSToolbar alloc] initWithIdentifier: @"Main
    Toolbar"] autorelease];
        //[mainToolbar setAllowsUserCustomization: YES];
        //[mainToolbar setAutosavesConfiguration: NO];
        [mainToolbar setDelegate: mainToolbarController];
        //[mainToolbar setVisible: YES];
        //[mainToolbar setDisplayMode: NSToolbarDisplayModeDefault];
        [window setShowsToolbarButton: YES];

        [window setToolbar: mainToolbar];

    Where mainToolbarController is an already instantiated object that
    manages the toolbar's items.  The problem is that a little inspection
    shows that [window toolbar] = nil but mainToolbar != nil after all
    this code runs, both later in windowControllerDidLoadNib: and in all
    later methods.

    I've tried a number of variations (moving the toolbar's delegate from/
    to the persistent document, autoreleasing and manually managing the
    memory for the mainToolbar, putting this code in awakFromNib:), but
    nothing seems to work.  Any suggestions on why I'm getting this
    strange behavior?

    -- -- -- -- -- -- -- -- -- -- -- -- -- --
                    Gordon Worley
                Phone: 352-875-5808
    e-mail: <redbird...>  PGP: 0xBBD3B003
      Web: http://homepage.mac.com/redbird/
  • I'm not 100% on this, but I think window itself is nil when you first
    enter -windowControllerDidLoadNib:. To make sure the window
    controller sets its 'window' variable, replace window with [self
    window].

        [[self window] setShowsToolbarButton: YES];
        [[self window] setToolbar: mainToolbar];

    It might also be worth NSLogging at the top of -
    windowControllerDidLoadNib: to see if I'm telling the truth:

    NSLog(@"before = %@", [window description]);
    NSLog(@"after = %@", [[self window] description]);

    Justin Anderson

    On Oct 13, 2005, at 10:26 AM, Gordon Worley wrote:

    > Hi, all!
    >
    > I'm having the following rather frustrating problem to which I
    > haven't found a solution by searching.  In an NSPersistentDocument
    > subclass, I try the following code in -windowControllerDidLoadNib: :
    >
    > //create the toolbar
    > mainToolbar = [[[NSToolbar alloc] initWithIdentifier: @"Main
    > Toolbar"] autorelease];
    > //[mainToolbar setAllowsUserCustomization: YES];
    > //[mainToolbar setAutosavesConfiguration: NO];
    > [mainToolbar setDelegate: mainToolbarController];
    > //[mainToolbar setVisible: YES];
    > //[mainToolbar setDisplayMode: NSToolbarDisplayModeDefault];
    > [window setShowsToolbarButton: YES];
    >
    > [window setToolbar: mainToolbar];
    >
    > Where mainToolbarController is an already instantiated object that
    > manages the toolbar's items.  The problem is that a little
    > inspection shows that [window toolbar] = nil but mainToolbar != nil
    > after all this code runs, both later in windowControllerDidLoadNib:
    > and in all later methods.
    >
    > I've tried a number of variations (moving the toolbar's delegate
    > from/to the persistent document, autoreleasing and manually
    > managing the memory for the mainToolbar, putting this code in
    > awakFromNib:), but nothing seems to work.  Any suggestions on why
    > I'm getting this strange behavior?
  • On Oct 13, 2005, at 10:54 AM, Justin Anderson wrote:

    > I'm not 100% on this, but I think window itself is nil when you
    > first enter -windowControllerDidLoadNib:. To make sure the window
    > controller sets its 'window' variable, replace window with [self
    > window].
    >
    > [[self window] setShowsToolbarButton: YES];
    > [[self window] setToolbar: mainToolbar];
    >

    I tried this, but NSPersistentDocument apparently doesn't respond to -
    window (at least that's the error I get), which I think is why I was
    referencing it the other way.

    -- -- -- -- -- -- -- -- -- -- -- -- -- --
                    Gordon Worley
                Phone: 352-875-5808
    e-mail: <redbird...>  PGP: 0xBBD3B003
      Web: http://homepage.mac.com/redbird/
  • On Oct 13, 2005, at 10:54 AM, Justin Anderson wrote:

    > It might also be worth NSLogging at the top of -
    > windowControllerDidLoadNib: to see if I'm telling the truth:
    >
    > NSLog(@"before = %@", [window description]);
    > NSLog(@"after = %@", [[self window] description]);

    Forgot to mention; window is nil at this point.  When does it get a
    value?

    -- -- -- -- -- -- -- -- -- -- -- -- -- --
                    Gordon Worley
                Phone: 352-875-5808
    e-mail: <redbird...>  PGP: 0xBBD3B003
      Web: http://homepage.mac.com/redbird/
  • As their may be several windows associated with a document, their is
    no method which returns one window.
    You can get all window controllers associated with the document with
    NSDocument's "windowControllers" method and then get the window from
    each controller.
    So if you're sure that you have at least one window, you can do the
    following in your "windowControllerDidLoadNib" implementation:
        NSWindow *window = [[[self windowControllers] objectAtIndex: 0]
    window];

    How does this work?

    Cheers,
    Tjark

    Am 13.10.2005 um 17:22 schrieb Gordon Worley:

    >
    > On Oct 13, 2005, at 10:54 AM, Justin Anderson wrote:
    >
    >
    >> It might also be worth NSLogging at the top of -
    >> windowControllerDidLoadNib: to see if I'm telling the truth:
    >>
    >> NSLog(@"before = %@", [window description]);
    >> NSLog(@"after = %@", [[self window] description]);
    >>
    >
    > Forgot to mention; window is nil at this point.  When does it get a
    > value?
    >
    > -- -- -- -- -- -- -- -- -- -- -- -- -- --
    > Gordon Worley
    > Phone: 352-875-5808
    > e-mail: <redbird...>  PGP: 0xBBD3B003
    > Web: http://homepage.mac.com/redbird/
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >
    >
  • On Oct 13, 2005, at 3:44 PM, <omnidevlist...> wrote:

    > As their may be several windows associated with a document, their
    > is no method which returns one window.
    > You can get all window controllers associated with the document
    > with NSDocument's "windowControllers" method and then get the
    > window from each controller.
    > So if you're sure that you have at least one window, you can do the
    > following in your "windowControllerDidLoadNib" implementation:
    > NSWindow *window = [[[self windowControllers] objectAtIndex: 0]
    > window];

    Thanks, that solved it!

    -- -- -- -- -- -- -- -- -- -- -- -- -- --
                    Gordon Worley
                Phone: 352-875-5808
    e-mail: <redbird...>  PGP: 0xBBD3B003
      Web: http://homepage.mac.com/redbird/
previous month october 2005 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today