【发布时间】:2011-07-29 16:44:47
【问题描述】:
我想知道如何在 Cocoa Mac Programming 中单击按钮打开一个新窗口。帮我。我正在做一个 mac 应用程序,它需要在特定按钮单击时打开一个新的 mac 窗口。
【问题讨论】:
我想知道如何在 Cocoa Mac Programming 中单击按钮打开一个新窗口。帮我。我正在做一个 mac 应用程序,它需要在特定按钮单击时打开一个新的 mac 窗口。
【问题讨论】:
Swift 3:在故事板中转到 WindowController -> 身份检查器 -> storyBoardID:填写:mainWindow。 然后从您当前的视图控制器将故事板上的按钮链接到以下方法:
@IBAction func newWindow(_ sender: Any) {
let myWindowController = self.storyboard!.instantiateController(withIdentifier: "mainWindow") as! NSWindowController
myWindowController.showWindow(self)
}
【讨论】:
如果你想为新窗口创建一个单独的类,这些是步骤:
按钮点击代码为:
NewWindowController *windowController = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"];
[windowController showWindow:self];
【讨论】:
- 创建一个类,它是 NSWindowController 的子类,例如新窗口控制器
- 为 NewWindowController 类创建一个窗口 xib。
按钮点击代码为:
NewWindowController *controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; [controllerWindow showWindow:self];
是的,但是如果此代码在某个函数内,则窗口会关闭。 这是解决方案。
在blah.h
@interface blah : NSObject {
...
NewWindowController *controllerWindow;
...
}
在blah.m
@implementation
...
-(IBAction)openNewWindow:(id)sender {
controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"];
[controllerWindow showWindow:self];
}
...
【讨论】:
NSWindowController * wc=[[NSWindowController alloc] initWithWindowNibName:@"your_nib_name"];
[wc showWindow:self];
【讨论】: