Getting pointer to NSWindowController

  • I am very new to cocoa and am trying to understand key value coding.  I have
    a document app wherein the NSDocument creates the NSWindowController which
    loads a NIB that contains a NSView inside an NSWindow.  The NSWindow
    contains the NSView and also a couple of NSTextFields bound to numerical
    properties owned by the NSWindowController.  I then display current mouse
    coordinates by using the following code in the NSView's mouse move handler:

        extern NSWindowController *wc;

        NSPoint loc = [event locationInWindow];

        loc.x -= [self frame].origin.x;

        loc.y -= [self frame].origin.y;

        [wc setValue:[NSNumber numberWithDouble:(double) loc.y] forKey:@"ypos"
    ];

        [wc setValue:[NSNumber numberWithDouble:(double) loc.x] forKey:@"xpos"];

    This works just fine when wc is a global pointer to the NSWindowController,
    which I added out of desperation.  But the following code, which I tried
    first, doesnt work for getting wc:

        NSWindow *w = [self window];

        NSWindowController *wc = [w windowController];

        produces NULL for wc

    It seems I must be doing something wrong since the NSWindow class have a
    property for NSWIndowController isnt documented to  always return NULL.  And
    it is hard to believe that the architects of cocoa intended to require that
    every bit of shared data be defined as global.

    Any chance my overall approach is wrong?  If so, what would be a more
    conventional approach for having events processed by a view result in
    updates to a control owned by the same window that owns the view?
  • On 8 Oct 2008, at 4:00 pm, Nirias wrote:

    > It seems I must be doing something wrong since the NSWindow class
    > have a
    > property for NSWIndowController isnt documented to  always return
    > NULL.  And
    > it is hard to believe that the architects of cocoa intended to
    > require that
    > every bit of shared data be defined as global.

    Your approach looks basically right to me.

    If [window windowController] is returning nil it may suggest that you
    haven't hooked everything together properly in IB. Have you connected
    the 'window' outlet of the controller to the window object? Forgetting
    to do that is common and very often things almost work anyway, so it
    might not leap out as an obvious problem.
    >

    hth, Graham
  • That was it.  Thanks.

    On Tue, Oct 7, 2008 at 11:41 PM, Graham Cox <graham.cox...> wrote:

    >
    > On 8 Oct 2008, at 4:00 pm, Nirias wrote:
    >
    > It seems I must be doing something wrong since the NSWindow class have a
    >> property for NSWIndowController isnt documented to  always return NULL.
    >> And
    >> it is hard to believe that the architects of cocoa intended to require
    >> that
    >> every bit of shared data be defined as global.
    >>
    >
    >
    > Your approach looks basically right to me.
    >
    > If [window windowController] is returning nil it may suggest that you
    > haven't hooked everything together properly in IB. Have you connected the
    > 'window' outlet of the controller to the window object? Forgetting to do
    > that is common and very often things almost work anyway, so it might not
    > leap out as an obvious problem.
    >
    >>
    >>
    >
    >
    > hth, Graham
    >