Help with Cocoa document app & controllers

  • I'm trying to make a cocoa document app. I have an NSDocument subclass
    "ChartDocument", and an NSWindowController subclass "ChartController".
    I have a nib file "ChartDocument.nib". It has a File's Owner,
    Application, First Responder and Window.

    The ChartController has an IBOutlet (IBOutlet ChartView* mChart) that
    points to a ChartView in the window. The ChartDocument provides:

    - (void)
    makeWindowControllers
    {
    ChartController* chartController = [[ChartController alloc] init];
    [self addWindowController: chartController];

    /* code to do some stuff that eventually needs the ChartView */
    }

    In the code represented by the comment above, the ChartController is
    called. It, in turn, tries to use the mChart IBOutlet that I wired up
    in IB. But it's null.

    Can anyone tell me why the mChart ivar isn't getting properly set? I'm
    sure I'm overlooking some basic thing.

    TIA,
    Rick
  • Rick,

    My guess is that the nib has not yet been loaded when you attempt to
    use the IBOutlet.  You can determine this for sure by calling -
    [NSWindowController isWindowLoaded], and you can load the nib by
    calling -[NSWindowController window], for example.

    -Jeff

    On Dec 26, 2007, at 7:10 PM, Rick Mann wrote:

    > I'm trying to make a cocoa document app. I have an NSDocument
    > subclass "ChartDocument", and an NSWindowController subclass
    > "ChartController". I have a nib file "ChartDocument.nib". It has a
    > File's Owner, Application, First Responder and Window.
    >
    > The ChartController has an IBOutlet (IBOutlet ChartView* mChart)
    > that points to a ChartView in the window. The ChartDocument provides:
    >
    > - (void)
    > makeWindowControllers
    > {
    > ChartController* chartController = [[ChartController alloc] init];
    > [self addWindowController: chartController];
    >
    > /* code to do some stuff that eventually needs the ChartView */
    > }
    >
    > In the code represented by the comment above, the ChartController
    > is called. It, in turn, tries to use the mChart IBOutlet that I
    > wired up in IB. But it's null.
    >
    > Can anyone tell me why the mChart ivar isn't getting properly set?
    > I'm sure I'm overlooking some basic thing.
    >
    > TIA,
    > Rick
    >
  • On Dec 26, 2007, at 5:24 PM, Jeff Johnson wrote:

    > My guess is that the nib has not yet been loaded when you attempt to
    > use the IBOutlet.  You can determine this for sure by calling -
    > [NSWindowController isWindowLoaded], and you can load the nib by
    > calling -[NSWindowController window], for example.

    You're right. It appears that calling NSWindowController's
    initWithNibName does not cause the nib to be loaded yet.

    Which means I can't do any real initialization in my controller's -
    init method. So, where am I supposed to do it? I know it's getting
    loaded eventually; the window shows up.

    TIA,
    --
    Rick
  • On Dec 26, 2007, at 5:41 PM, Rick Mann wrote:

    > Which means I can't do any real initialization in my controller's -
    > init method. So, where am I supposed to do it? I know it's getting
    > loaded eventually; the window shows up.
    >
    <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSWindowController_Class/Reference/Reference.html#//apple_ref/occ/inst
    m/NSWindowController/windowDidLoad
    >

    mmalc
  • On Dec 26, 2007, at 5:57 PM, mmalc crawford wrote:

    > On Dec 26, 2007, at 5:41 PM, Rick Mann wrote:
    >
    >> Which means I can't do any real initialization in my controller's -
    >> init method. So, where am I supposed to do it? I know it's getting
    >> loaded eventually; the window shows up.
    >>
    > <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSWindowController_Class/Reference/Reference.html#//apple_ref/occ/inst
    m/NSWindowController/windowDidLoad
    > >

    I looked at that, but it doesn't let me initialize the window from my
    document subclass.

    --
    Rick
  • On Dec 26, 2007, at 6:43 PM, Rick Mann wrote:

    >>> Which means I can't do any real initialization in my controller's -
    >>> init method. So, where am I supposed to do it? I know it's getting
    >>> loaded eventually; the window shows up.
    >>>
    >> <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSWindowController_Class/Reference/Reference.html#//apple_ref/occ/inst
    m/NSWindowController/windowDidLoad
    >> >
    >
    > I looked at that, but it doesn't let me initialize the window from
    > my document subclass.
    >
    Well, you asked "Where are you supposed to do it", to which the
    typical answer is "in windowDidLoad".
    If you really want to do initialization from the document subclass,
    then per Jeff's earlier reply you need to ensure the window is loaded
    first, in which case simply invoke 'window'.

    mmalc
  • The Init method does not get called for controllers in your nib.  (Just
    ran into that myself recently.)  You need initWihCoder or else do what you
    need in awakeFromNib if it needs to have the connections first.
    (Connections are guaranteed to be established before awakeFromNib gets
    called.)

    > To: Jeff Johnson <publicposting...>
    > Cc: <Cocoa-dev...>
    > Message-ID: <925D03A3-6674-4625-95F4-2BCEE6861443...>
    > Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
    >
    >
    > On Dec 26, 2007, at 5:24 PM, Jeff Johnson wrote:
    >
    >> My guess is that the nib has not yet been loaded when you attempt to
    >> use the IBOutlet.  You can determine this for sure by calling -
    >> [NSWindowController isWindowLoaded], and you can load the nib by
    >> calling -[NSWindowController window], for example.
    >
    > You're right. It appears that calling NSWindowController's
    > initWithNibName does not cause the nib to be loaded yet.
    >
    > Which means I can't do any real initialization in my controller's -
    > init method. So, where am I supposed to do it? I know it's getting
    > loaded eventually; the window shows up.
    >
    >
    > TIA,
    > --
    > Rick
    >
  • On Dec 26, 2007, at 7:47 PM, Gordon Apple wrote:

    > The Init method does not get called for controllers in your nib.
    > (Just
    > ran into that myself recently.)  You need initWihCoder or else do
    > what you
    > need in awakeFromNib if it needs to have the connections first.
    > (Connections are guaranteed to be established before awakeFromNib gets
    > called.)
    >
    This is wrong and misleading.
    (a) initWithCoder is not called for a standard NSWindowController
    instance.
    (b) NSWindowController specifies a particular method to override to
    perform initalisation after the interface is loaded -- see <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSWindowController_Class/Reference/Reference.html#//apple_ref/occ/inst
    m/NSWindowController/windowDidLoad
    >.

    mmalc
  • Hi Rick
    >
    > In the code represented by the comment above, the ChartController is
    > called. It, in turn, tries to use the mChart IBOutlet that I wired up
    > in IB. But it's null.
    At this point (in makeWindowControllers method) the nib is not yet
    loaded.
    Thats why you are seeing the outlet value as nil.

    >
    > Can anyone tell me why the mChart ivar isn't getting properly set? I'm
    > sure I'm overlooking some basic thing.
    Do whatever initializations in the
    - (void)windowDidLoad;
    of your window controller. That should help.

    Regards
    Shripada
previous month december 2007 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