macos 如何在 Cocoa Mac 应用程序中单击按钮打开一个新窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5547741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a new window on button click in Cocoa Mac Application?
提问by ShinuShajahan
I want to know how to open a new window on button click in Cocoa Mac Programming. Help me. I am doing a mac application which needs to open a new mac window on particular button click.
我想知道如何在 Cocoa Mac Programming 中点击按钮打开一个新窗口。帮我。我正在做一个 mac 应用程序,它需要在单击特定按钮时打开一个新的 mac 窗口。
回答by iPhoneDv
If you want to create a separate class for New Window, these are the steps:
如果你想为新窗口创建一个单独的类,这些是步骤:
- Create a class which is a sub class of NSWindowController e.g. NewWindowController
- Create a window xib for NewWindowController class.
On button click code as:
NewWindowController *windowController = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; [windowController showWindow:self];
- 创建一个类,它是 NSWindowController 的子类,例如 NewWindowController
- 为 NewWindowController 类创建一个窗口 xib。
在按钮上单击代码为:
NewWindowController *windowController = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; [windowController showWindow:self];
回答by Saurabh
NSWindowController * wc=[[NSWindowController alloc] initWithWindowNibName:@"your_nib_name"];
[wc showWindow:self];
回答by Hans
Swift 3: In your storyboard go to WindowController -> Identity inspector -> storyBoardID: fill out: mainWindow. Then from your current viewcontroller link the button on the storyboard to the following method:
Swift 3:在您的故事板中,转到 WindowController -> Identity inspector -> storyBoardID:填写:mainWindow。然后从您当前的视图控制器将故事板上的按钮链接到以下方法:
@IBAction func newWindow(_ sender: Any) {
let myWindowController = self.storyboard!.instantiateController(withIdentifier: "mainWindow") as! NSWindowController
myWindowController.showWindow(self)
}
回答by WildMassacre
- Create a class which is a sub class of NSWindowController e.g. NewWindowController
- Create a window xib for NewWindowController class.
On button click code as:
NewWindowController *controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; [controllerWindow showWindow:self];
- 创建一个类,它是 NSWindowController 的子类,例如 NewWindowController
- 为 NewWindowController 类创建一个窗口 xib。
在按钮上单击代码为:
NewWindowController *controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; [controllerWindow showWindow:self];
Yes, but the window closes if this code is inside of some func. Here is solution.
是的,但是如果此代码在某个 func 内,则窗口将关闭。这是解决方案。
In blah.h
在 blah.h
@interface blah : NSObject {
...
NewWindowController *controllerWindow;
...
}
In blah.m
在 blah.m
@implementation
...
-(IBAction)openNewWindow:(id)sender {
controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"];
[controllerWindow showWindow:self];
}
...