So this is how I solved it...
I have a class "MyPreferencesWindowController" which has a method called getInstance
. This method is what is called when you want to get the preferences window. This solution takes advantage of singleton technology.
/**
Method in my MyPreferencesWindowController.m file
with a corresponding method in the .h file.
*/
+(id) getInstance {
static PreferencesWindowController *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
现在,在文件类别中,当你想展示优惠窗口时,我们做以下工作:
-(IBAction) showPreferences:(id)sender {
if (preferencesWc == nil)
preferencesWc = [MyPreferencesWindowController getInstance];
[ preferencesWc showWindow:self ];
}
This will ensure the preferences window is only created once. And then each call to getInstance
will return the same instance of the window.