NSWindowController 显示新窗口

发布于 2024-12-03 22:02:18 字数 369 浏览 0 评论 0原文

我对 mac 编程很陌生。三天前刚刚开始。
我正在制作一个示例应用程序,其中主窗口中有一个按钮
我正在使用此代码打开一个新的 wndowcontroller

ThirdViewController *tvc = [[ThirdViewController alloc] initWithWindowNibName:@"SecondViewController"];

     [tvc showWindow:self];

这工作正常,但是当我再次按下按钮时,它将再次打开相同的窗口,因此每次单击后我都会在屏幕上显示+1个窗口。
我想要的是,如果我的新窗口已经在我的屏幕上,那么按钮无法添加相同的窗口。

提前致谢:)

I am very new to mac programming. Just started before 3 days.
I am making a sample app in which i have one button in main window
I am using this code to open a new wndowcontroller

ThirdViewController *tvc = [[ThirdViewController alloc] initWithWindowNibName:@"SecondViewController"];

     [tvc showWindow:self];

This working fine but when i press button again it will open same window again so after every click i have +1 window on screen.
What i want is if my new window is already on my screen then button can't add same window.

Thanks in advance:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2024-12-10 22:02:18

如果每当单击按钮时都会执行该代码,那么您实际上是在创建一个新的窗口控制器,从 nib 文件加载其窗口,并在单击按钮时多次显示该窗口。

防止这种情况发生的标准方法是使用一个最初为 nil 的实例变量,并仅为其分配一次窗口控制器。随后,实例变量不再是nil,您可以对其进行测试以避免创建另一个控制器并再次加载 nib 文件。

例如,您可以在应用程序委托或负责第三个窗口控制器的任何控制器中声明以下实例变量:

ThirdViewController *tvc;

并且,当单击按钮时:

if (nil == tvc) {
    // If tvc is nil then it's the first time this code is being executed
    tvc = [[ThirdViewController alloc] initWithWindowNibName:@"SecondViewController"];
}

[tvc showWindow:self];

If that code is being executed whenever the button is clicked then you’re effectively creating a new window controller, loading its window from a nib file, and showing that window as many times as the button is clicked.

The standard approach to prevent this from happening is having an instance variable that is initially nil and assigning it a window controller only once. Subsequently, the instance variable is not nil any longer and you can test that to avoid creating another controller and loading the nib file again.

You could, for example, declare the following instance variable in your application delegate or whatever controller should be responsible for the third window controller:

ThirdViewController *tvc;

and, when the button is clicked:

if (nil == tvc) {
    // If tvc is nil then it's the first time this code is being executed
    tvc = [[ThirdViewController alloc] initWithWindowNibName:@"SecondViewController"];
}

[tvc showWindow:self];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文