2016-04-27 35 views
0

[首先,請注意我沒有使用Interface Builder]Modal NSWindow從未發佈

我有一個NSWindow在菜單項被點擊時顯示爲模式表。下面的代碼顯示它:

let win = NSWindow(contentRect: NSRect(origin: NSZeroPoint, size: NSSize(width: 650, height: 400)), styleMask: NSResizableWindowMask, backing: .Nonretained, defer: false) 
win.releasedWhenClosed = true 
setupController = SetupSheetController(window: win) 
mainWindow.beginSheet((setupController!.window)!) { (response: NSModalResponse) in 
    print("didClose") 
    self.setupController = nil 
} 

setupController被定義爲一個實例變量var setupController: SetupSheetController?;和SetupSheetControllerNSWindowController子類。

當用戶完成此表格時,self.window?.sheetParent?.endSheet(self.window!, returnCode: NSModalResponseXXX)setupController調用。

我的問題是,模態NSWindow和它的控制器都沒有獲得釋放。當我重新打開模態表單時,它們也不會被重用。 另請注意,我沒有在此處顯示的其他地方參考控制器。 想法?

回答

0

好的......原來我不得不nil窗外本身,雖然使用一些間接:win.windowController?.window = nil(要寫在beginSheet的completionHandler)。

但它看起來對我來說不是很優雅。有沒有其他方法?

+0

我認爲一般的想法是,一旦創建了窗口,它們應該在應用程序生命週期中重用。創建窗口很昂貴,因此默認行爲是在第一次請求顯示時懶惰地創建窗口,然後在完成後隱藏窗口。當然,這也需要您保持對控制器的持續參考。 – pco494