找到你要的答案

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

Q:在OS X = 10.10显示窗口(插件模板)?

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?

有很多类似的问题,但他们似乎太老了-什么也没有发生,beginsheet:…方法声明的,等我需要像下面的(但在OS X窗口):

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

比如我创建nswindowcontroller与XIB通过相似的方式运行nsapp beginsheet:[…]但它落在任何beginsheet:…方法.但这个例子甚至在苹果文档中描述。

我做错了什么?是因为我使用的插件模板下载通过恶魔岛没有任何窗口,但从一开始就应该在菜单项,单击“显示”吗?

answer1: 回答1:

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

你错误地使用子类的nsviewcontroller,即“myviewcontroller”。你需要的是nswindowcontroller子类。作为一个表只能使用NSWindow(不nsview)。如果你想运行窗口模式(这意味着依赖窗口)确保存在窗口显示。

当没有窗口可以实现与XIB创造NSWindowController新的子类。然后实例化这样:

    @interface SomeClass {
      CustomWindowController *customWindowController;
    }

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

更多信息关于表:使用应用程序对话框

osx  window  xib  nswindow  nswindowcontroller