如何使用按钮在可可mac应用程序中打开一个新窗口 [英] How to open a new window in cocoa mac application with a button

查看:221
本文介绍了如何使用按钮在可可mac应用程序中打开一个新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做一个简单的应用程序,当按下一个按钮,一个新的窗口应该出现。
我尝试过

I have to do a simple app, that when a button is pressed, a new window should appear. I've tried with

- (IBAction)LanciaPavia:(id)sender {
    NSWindowController *controllerWindow = [[NSWindowController alloc] initWithWindowNibName:@"AOPAVIAWindowController"];
    [controllerWindow showWindow:self];
}

但新窗口出现并立即关闭。我需要做什么?我不知道要继续,我是新的可可世界!

but the new window appears and immediately closes. What do I have to do? I don't know to proceed, and I'm new to the cocoa world!

推荐答案

答案的关键是范围:

- (IBAction)LanciaPavia:(id)sender {
    NSWindowController *controllerWindow = [[NSWindowController alloc] initWithWindowNibName:@"AOPAVIAWindowController"];
    [controllerWindow showWindow:self];
}    // controllerWindow goes out of scope

c $ c> controllerWindow 会超出范围(我假设你使用ARC),所以窗口控制器被销毁,窗口关闭。

At the end of that scope the controllerWindow would go out of scope (I assume you are using ARC) and so the window controller be destroyed and the window closed.

将它设置为一个实例变量,最好只创建一次。

Make it an instance variable and preferably one that is only created once.

这篇关于如何使用按钮在可可mac应用程序中打开一个新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆