[首先,請注意我沒有使用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?
;和SetupSheetController
是NSWindowController
子類。
當用戶完成此表格時,self.window?.sheetParent?.endSheet(self.window!, returnCode: NSModalResponseXXX)
被setupController
調用。
我的問題是,模態NSWindow和它的控制器都沒有獲得釋放。當我重新打開模態表單時,它們也不會被重用。 另請注意,我沒有在此處顯示的其他地方參考控制器。 想法?
我認爲一般的想法是,一旦創建了窗口,它們應該在應用程序生命週期中重用。創建窗口很昂貴,因此默認行爲是在第一次請求顯示時懶惰地創建窗口,然後在完成後隱藏窗口。當然,這也需要您保持對控制器的持續參考。 – pco494