Main Window Disappears when Loaded from Separate NIB File

  • I hope someone can put me straight. I created a vanilla Cocoa App. I deleted the window from the MainMenu.xib and created a MainWindow.xib with essentially an identical window (no views).

    I created a MainWindowController with one overriding method, init, see below. In the app delegate's applicationDidFinishLaunching method I create the main window controller and attempt to show the main window. If I breakpoint just after the [mainController showWindow: nil] method I can see the proper user interface with the main menu and main window. However, when I let the program run the window immediately disappears. As none of my code runs after that point I'm at a bit of a loss discovering what makes the window go away.

    What am I doing (or not doing) that causes the main window to close or become invisible? The code is below.

    Thanks,

    Tom Wetmore

    AppDelegate.h:
    --------------
    @interface AppDelegate : NSObject <NSApplicationDelegate>
    @end

    AppDelegate.m:
    --------------
    #import "AppDelegate.h"
    #import "MainWindowController.h"

    @implementation AppDelegate

    - (void) applicationDidFinishLaunching: (NSNotification*) aNotification {
        MainWindowController* mainController = [[MainWindowController alloc] init];
        [mainController window]; // force controller to load nib.
        [mainController showWindow: nil];  // show window -- which promptly disappears
    }

    @end

    MainWindowController.h:
    -----------------------
    @interface MainWindowController : NSWindowController
    @end

    MainWindowController.m:
    -----------------------
    #import "MainWindowController.h"

    @implementation MainWindowController

    - (id) init { return [super initWithWindowNibName: @"MainWindow"]; }

    @end
  • ARC is on. Retain the window controller.

    On Tue, May 7, 2013 at 4:52 PM, Thomas Wetmore <ttw4...> wrote:
    > I hope someone can put me straight. I created a vanilla Cocoa App. I deleted the window from the MainMenu.xib and created a MainWindow.xib with essentially an identical window (no views).
    >
    > I created a MainWindowController with one overriding method, init, see below. In the app delegate's applicationDidFinishLaunching method I create the main window controller and attempt to show the main window. If I breakpoint just after the [mainController showWindow: nil] method I can see the proper user interface with the main menu and main window. However, when I let the program run the window immediately disappears. As none of my code runs after that point I'm at a bit of a loss discovering what makes the window go away.
    >
    > What am I doing (or not doing) that causes the main window to close or become invisible? The code is below.
    >
    > Thanks,
    >
    > Tom Wetmore
    >
    > AppDelegate.h:
    > --------------
    > @interface AppDelegate : NSObject <NSApplicationDelegate>
    > @end
    >
    > AppDelegate.m:
    > --------------
    > #import "AppDelegate.h"
    > #import "MainWindowController.h"
    >
    > @implementation AppDelegate
    >
    > - (void) applicationDidFinishLaunching: (NSNotification*) aNotification {
    > MainWindowController* mainController = [[MainWindowController alloc] init];
    > [mainController window]; // force controller to load nib.
    > [mainController showWindow: nil];  // show window -- which promptly disappears
    > }
    >
    > @end
    >
    >
    > MainWindowController.h:
    > -----------------------
    > @interface MainWindowController : NSWindowController
    > @end
    >
    > MainWindowController.m:
    > -----------------------
    > #import "MainWindowController.h"
    >
    > @implementation MainWindowController
    >
    > - (id) init { return [super initWithWindowNibName: @"MainWindow"]; }
    >
    > @end
  • Steven,

    Thanks! That did it.

    Tom Wetmore

    On May 7, 2013, at 5:57 PM, Steven Degutis <sbdegutis...> wrote:

    > ARC is on. Retain the window controller.
    >
    > On Tue, May 7, 2013 at 4:52 PM, Thomas Wetmore <ttw4...> wrote:
    >> I hope someone can put me straight. I created a vanilla Cocoa App. I deleted the window from the MainMenu.xib and created a MainWindow.xib with essentially an identical window (no views).
    >>
    >> I created a MainWindowController with one overriding method, init, see below. In the app delegate's applicationDidFinishLaunching method I create the main window controller and attempt to show the main window. If I breakpoint just after the [mainController showWindow: nil] method I can see the proper user interface with the main menu and main window. However, when I let the program run the window immediately disappears. As none of my code runs after that point I'm at a bit of a loss discovering what makes the window go away.
    >>
    >> What am I doing (or not doing) that causes the main window to close or become invisible? The code is below.
previous month may 2013 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