Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSWindowController to show a window in standard application?

I created a new blank standard application using Xcode template. Removed the window in MainMenu.xib and I created a new customized NSWindowController subclass with a xib.

They were named "WYSunFlowerWindowController.h" and "WYSunFlowerWindowController.m".

And I append then init function like below:

- (id)init
{
    NSLog(@"init()");

    return [super initWithWindowNibName:@"WYSunFlowerWindowController" owner:self];
}

And my WYAppDelegate.m file is like below:

static WYSunFlowerMainWindowController* windowController = nil;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    if (windowController == nil) {
        windowController = [[WYSunFlowerMainWindowController alloc] init];
    }
    [[windowController window] makeKeyAndOrderFront:windowController];
}

And I have the problem, that the window can't show it self after I launch the app. Anyone can tell me why? Is anything wrong with my code?

I am a newbie in Objective-C and cocoa. So I think I maybe make a silly mistake that I can't figure it out by myself.

UPDATE:

Here is my project source. Pleas have a look and help me to figure out what is my mistake。

https://dl.dropbox.com/u/3193707/SunFlower.zip

like image 900
morphinewan Avatar asked Nov 04 '22 15:11

morphinewan


1 Answers

In your init method, I think you have to set self to the super init first before you return self.

-(id)init
{
    NSLog (@"init()");
    self = [super initWithWindowNibName:@"WYSunFlowerWindowController" owners:self];
    return self;
}

Edit:

Try replace makeKeyAndOrderFront: with [windowController showWindow:self]

Then if that still doesn't work, check your window controller xib, make sure the file owner is set to WYSunFlowerWindowController and that the IBOutlet Window (declared in NSWindowController) is connected to the window.

Edit 2:

Commenting out your @property and @synthesize window in your controller was the trick. Don't redeclare get and setters that were already predefined in a superclass.

like image 117
TheAmateurProgrammer Avatar answered Nov 14 '22 17:11

TheAmateurProgrammer