簡體   English   中英

NSWindowDelegate 沒有收到調整大小的通知

[英]NSWindowDelegate not getting resize notifications

在 macOS Cocoa 應用程序中,我試圖通過設置 NSWindow 委托在我的窗口“角拖動”到新大小時獲取通知。 最初創建窗口時,我成功地收到了調整大小事件的通知,但后來將窗口拖動到新大小時卻沒有。 我不知道為什么不。

這是我的代碼:

class MyWindowController: NSWindowController {
    override func windowDidLoad() {
        super.windowDidLoad()
        window?.delegate = self
    }
}

extension MyWindowController: NSWindowDelegate {
    func windowDidResize(_ notification: Notification) {
        print("windowDidResize")
    }
    
    func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
        print("windowWillResize")
        return frameSize
    }
}

首次創建窗口時,我看到以下輸出:

windowWillResize
windowDidResize

這證明了委托方法是有效的。 但是,當我抓住窗口的一角並調整它的大小時,我看不到任何額外的print輸出。

我已經在 SO 上查看了一些關於獲取這些通知(如thisthis )的類似問題,看起來我做的一切都是正確的。 但是我沒有收到關於窗口角拖動調整大小的通知。 有誰知道為什么?

根據 Willeke 的評論,我在我的 AppDelegate(創建窗口的位置)中創建了對 NSWindowController 子類的強引用,它解決了問題。

對於將來發現這一點的人,這就是我在強參考中創建的方式(在 AppDelegate 中):

// Need to maintain a strong reference while window is open
var myWindowController : MyWindowController?

[...]

func funcThatCreatesWindow() {
  [...]
  self.myWindowController = storyboard.instantiateController(withIdentifier: storyboardID) as? MyWindowController
  if self.myWindowController != nil {
    myWindowController!.showWindow(nil)
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM