NSWindowController 显示新窗口
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果每当单击按钮时都会执行该代码,那么您实际上是在创建一个新的窗口控制器,从 nib 文件加载其窗口,并在单击按钮时多次显示该窗口。
防止这种情况发生的标准方法是使用一个最初为 nil 的实例变量,并仅为其分配一次窗口控制器。随后,实例变量不再是
nil
,您可以对其进行测试以避免创建另一个控制器并再次加载 nib 文件。例如,您可以在应用程序委托或负责第三个窗口控制器的任何控制器中声明以下实例变量:
并且,当单击按钮时:
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 notnil
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:
and, when the button is clicked: