Objective c 如何从Cocoa中的另一个NSWindowController打开NSWindowController
我正在Xcode5中为MacOSX开发cocoa应用程序,我想通过按下按钮从当前窗口打开另一个窗口,这是我的代码:Objective c 如何从Cocoa中的另一个NSWindowController打开NSWindowController,objective-c,macos,cocoa,xcode5,Objective C,Macos,Cocoa,Xcode5,我正在Xcode5中为MacOSX开发cocoa应用程序,我想通过按下按钮从当前窗口打开另一个窗口,这是我的代码: - (IBAction)openWindow:(id)sender { WindowController *controllerWindow = [[WindowController alloc] initWithWindowNibName:@"WindowController"]; [controllerWindow showWindow:nil]; [
- (IBAction)openWindow:(id)sender {
WindowController *controllerWindow = [[WindowController alloc] initWithWindowNibName:@"WindowController"];
[controllerWindow showWindow:nil];
[[controllerWindow window] makeMainWindow];
}
到目前为止,我可以看到窗口出现了一秒钟,然后它就消失了,如何正确地做到这一点?窗口和窗口控制器在该方法范围之外的任何地方都没有强引用。 在那之后,他们就被释放了 通常,您会将窗口控制器添加到一些容器中,如应用程序委托中的数组。 阵列将保留窗口控制器。 窗口控制器可以挂在窗口上 动作方法在app委托中也有意义。您只需在应答器链上发送一个选择器即可。使用此按钮 创建一个需要打开的新的.h和.m文件,如NewWindowController(例如)及其.xib 点击任何按钮,打开新定义的窗口,只需分配、实例化和呈现
NewWindowController *controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"NewWindowController"];
[controllerWindow showWindow:self];
可能重复的,不要忘了先初始化数组!