Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using NSWindowController

I just started a new Cocoa project after a long time... And I don't know why, but I always get an error when calling a xib by a NSWindowController. What I do is really very simple: I have a new project as a starting point and then I don't wantz to call the xib from Appdelegate, but from a subclass of NSWindowController. Then the output tells me that:

2014-11-12 09:58:18.519 SimpleTest[8554:378690] ApplePersistence=NO

2014-11-12 09:58:18.671 SimpleTest[8554:378690] Failed to connect (window) outlet from (NSApplication) to (NSWindow): missing setter or instance variable

Okay, how does it look in code? My Appdelegate looks like this:

#import "AppDelegate.h"
#import "MainWindowController.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (strong) MainWindowController *mainWindowController;

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    _mainWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainMenu"];
    [self.mainWindowController showWindow:self];
}

@end

Nothing special so far. The MainWindowController looks like this:

#import "MainWindowController.h"

@interface MainWindowController ()
@property (weak) IBOutlet NSWindow *window;

@end

@implementation MainWindowController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];

    if (self != nil)
    {
        //do something
    }

    return self;
}

@end

And again very simple... Additiponally I have some modifications in IB: File'Owner of MainMenu.xib becomes MainWindowController. Its 'window' outlet is connected to the Window of the application. The delegate of Window is connected to File's Owner. Well, that's it! But why do I receive this error? What am I doing wrong?

---EDIT--- this shows the connections in IB

conncections

like image 859
usermho Avatar asked Nov 01 '22 14:11

usermho


1 Answers

The most important is using right selector to create new instance and having all wired up correctly.

Steps: 1. Add new xib with window or an empty one and add window to it

enter image description here

2.Select File owner and set it to NSWindowController or its subclass.

enter image description here

  1. Select window and connect it to File owner

enter image description here

  1. Make new NSWindowController instance

  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  {
      NSWindowController *windowController = [[NSWindowController alloc] initWithWindowNibName:@"Window"];
      [NSApp runModalForWindow:[windowController window]];
  }

enter image description here

like image 75
Marek H Avatar answered Nov 11 '22 09:11

Marek H