2011-12-20 68 views
2

我正在處理基於NSDocument的應用程序,每個窗口有多個文檔(選項卡)。這意味着我需要自己處理關閉窗口,以便在窗口關閉之前查看屬於窗口的文檔。爲了做到這一點,我使用standardWindowButton:NSWindowCloseButton訪問了NSWindow的關閉按鈕,並將此按鈕的目標/操作設置爲我的方法,而不是標準(和專用)方法。窗口關閉按鈕劫持雪豹

這對獅子很有用,但在雪豹上會引起問題。無論何時顯示模態對話框,關閉按鈕都會按預期禁用。但是當模式對話框關閉時,在雪豹上,關閉按鈕永遠不會被重新啓用。我嘗試過使用[closeButton setEnabled:YES]等編程後重新啓用它,但它似乎沒有任何效果。我已經證實,只有當我更改關閉按鈕的目標/操作時纔會發生這種情況。

關於如何避免Snow Leopard上的這種行爲,或者可能是劫持關閉按鈕的替代方法的任何想法?它是什麼控制工具欄按鈕的啓用狀態?也許我可以重寫那裏的東西?

+1

如果在顯示對話框之前刪除目標和操作並在對話框關閉時添加它,會發生什麼? – 2011-12-20 17:49:33

+0

我以爲我試過了,但現在我做到了,它看起來像是有效的!雖然有點棘手,但要準確找出何時爲所有不同的模式對話框添加和刪除目標/動作。謝謝。 – krill 2011-12-20 19:44:40

回答

4

我還以爲你可以使用windowShouldClose:委託方法

設置Windows委託給AppDelegate中。在AppDelegate中,使用windowShouldClose:delegate方法調用close方法,並通過返回NO來停止窗口關閉。 在你的方法中做所有的檢查,然後執行關閉:窗口。查看我的示例

NSWindow * thisWindow; //--pointer to window that will be closed 
BOOL windowClose;//-- bool for confirming close of window. 

- (BOOL)windowShouldClose:(id)sender{ 
    thisWindow =sender;//-- set thisWindow to the sender window,the one that is to be closed) 
    //if (sender ==theWindow) {//--you can use this to do further checking 

     if (windowClose) {//-- Close window if YES 
      return YES; 
     } 
    //} 


    [self performSelector:@selector(myCloseWindow) ];//go to your method 
    windowClose =FALSE;//-- reset 
    return NO;//do not close window here 
} 

- (void) myCloseWindow { 
    NSLog(@"closing window");//-- do your stuff 
    windowClose =TRUE;//--give the ok to close the window 
    [thisWindow performClose:thisWindow];//-- perform the close, which will be redirected back to the delegate, which will now allow the window to close 
} 
+0

您可能會期望這能夠奏效,但這並不是因爲這個委託方法在窗口實際被關閉之前被調用,而是與基於文檔的應用中的窗口關閉按鈕不同。例如,如果文檔未保存,文檔體系結構會嘗試要求用戶在文檔嘗試關閉窗口之前保存該文檔,因此只有在嘮叨用戶之後纔會調用該委託方法,這太遲了。 – krill 2012-01-11 02:01:24