NSWindowController shows new window
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 wndowcontrollerThirdViewController *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:)
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];
精彩评论