Search code examples
macoswindowxibnswindownswindowcontroller

Show window in OS X >= 10.10 (plugin template)?


There are a lot of similar questions but they appear to be too old - nothing happens, beginSheet:... methods undeclared and etc. I need something like the following (but for window in OS X):

//just created class with option "also create xib"
MyViewController *vc = [[MyViewController alloc] initWithNibName:@"..." bundle:nil];
[self.navigationController pushViewController:controller animated:NO];

So for example I create NSWindowController with xib via the similar way and run [NSApp beginSheet:...] but it falls down on any from beginSheet:... methods. But this example is even described in apple documentation.

What I'm doing wrong? Is it because of I use plugin template downloaded via Alcatraz which doesn't have any window from the beginning but should show it on menu item click?


Solution

  • You incorrectly used subclass of NSViewController, namely "MyViewController". What you need is a subclass of NSWindowController. As a sheet you can only use NSWindow (not NSView). If you are trying to run window modally (this means having dependant window) make sure there is existing window displayed.

    The case when there is no window you can achieve with creating new subclass of NSWindowController with a XIB. Then instantiate like this:

        @interface SomeClass {
          CustomWindowController *customWindowController;
        }
    
        - (IBAction)createNewWindow:(id)sender {
          customWindowController = [[CustomWindowController alloc] initWithWindowNibName:@"NameOfXib"];
          [customWindowController showWindow:nil];
        }
    

    More info about sheets: Using Application-Modal Dialogs