In a macOS Cocoa app, I am trying to get notifications when my window is "corner dragged" to a new size by setting an NSWindow delegate. I am successfully getting notified of resize events when the window is initially created, but not when the window is later dragged to a new size. I can't figure out why not.
Here is my code:
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
}
}
When the window is first created, I see this output:
windowWillResize
windowDidResize
which proves the delegate methods are working. However, when I then grab a corner of the window and resize it, I do not see any additional print
output.
I have reviewed a number of similar questions on SO about getting these notifications (like this and this ), and it seems like I am doing everything right. And yet I don't get the notifications on window corner drag resize. Does anyone know why?
Based on the comment from Willeke, I created a strong reference to the NSWindowController subclass in my AppDelegate (where the window is created) and it fixed the problem.
For people finding this in the future, this is how I created in the strong reference (in 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)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.