This question is about a macOS app, not iOS.
I have a basic Cocoa app that supports multiple windows. I noticed that NSViewController.viewWillAppear() and viewDidAppear() are not called when the app launches and the window was minimized to the dock previously.
To reproduce:
- Launch the app and open a new window
- Minimize the window to the dock (Window → Minimize)
- Quit and relaunch the app
→ The window should still be minimized in the dock
- Click on the window in the dock to bring it back on screen
Expected:
NSViewController.viewWillAppear()andviewDidAppear()should be called
Actual:
- they are not called
This is a problem, because I need a reference to the NSWindow in my view controller to finish some setup so I can't simply hook into viewDidLoad().
Minimal, reproducible example
- System Preferences → General → Uncheck "Close windows when quitting an app"
- Xcode → New Project → macOS App:
- User Interface: Storyboard
- ☑︎ Create Document-Based Application
- Open
ViewController.swiftand add:
.
override func viewWillAppear() {
super.viewWillAppear() [PUT BREAKPOINT HERE]
}
- Open
Document.swiftand:- Have
func data(ofType typeName: String)simply returnData() - Have
func read(from data: Data, ofType typeName: String)do nothing (remove the exception)
- Have
- Build & run
- Cmd-S to save the document that opened by default
- Window → Minimize
- Document window now minimized in the dock
- Cmd-Q to quit the app
- Relaunch the app
- Document window still sits minimized in the dock
- Click on the document window icon in the dock to bring it back
viewWillAppear() and viewDidAppear() will not be called as the window is unminimized.
windowDidLoadormakeWindowControllers. - Willeke