所以這是我如何解決它...... 我有一個類「MyPreferencesWindowController」其中有一個叫getInstance
方法。這個方法就是你想要獲得偏好窗口時所調用的方法。該解決方案利用單件技術。
/**
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 ];
}
這將確保只創建一次Preferences窗口。然後每次撥打getInstance
將返回窗口的相同實例。
謝謝!你真的幫我! –
我需要幫助!當我使用上面的評論代碼時,在我的應用程序中,我可能只打開一個「首選項」窗口。那就對了!!!但!當我關閉他時,再次按下「命令」,這不起作用。如何解決它? –
在該窗口的界面生成器中,確保'關閉時釋放'未被選中,這樣當關閉時他的剛纔隱藏的首選項窗口就會被隱藏起來。 –