CodexBloom - Programming Q&A Platform

NSViewController's viewWillAppear not called when switching between NSWindowControllers on macOS 13.6

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-08-24
macos swift nsviewcontroller

I'm refactoring my project and I'm relatively new to this, so bear with me... I've looked through the documentation and I'm still confused about I'm having an scenario where `viewWillAppear` is not getting called when I switch between two `NSWindowControllers` in my macOS 13.6 application. I have two windows that are instantiated from different view controllers, and I'm trying to manage their appearance and lifecycle methods correctly. Here's a snippet of what I'm doing: ```swift class FirstWindowController: NSWindowController { override func windowDidLoad() { super.windowDidLoad() // Additional setup } override func showWindow(_ sender: Any?) { super.showWindow(sender) } } class SecondWindowController: NSWindowController { override func windowDidLoad() { super.windowDidLoad() } } ``` When I create an instance of `FirstWindowController` and then switch to `SecondWindowController`, I expect `viewWillAppear` to be called for the second window, but it isn't. I have verified that the window is being presented correctly and that the `NSViewController` is initialized, yet none of its lifecycle methods are triggered. I've ensured that I am using `showWindow` to display the new window controller correctly, but it seems like I missed something regarding how these controllers are supposed to interact with each other. I also tried calling `self.contentViewController?.viewWillAppear()` directly in the `showWindow` method of the `SecondWindowController`, but it still doesn't seem to work as intended. The only output I see in the console is `NSViewController: viewWillAppear is not called`. Any insights on what I might be doing wrong or any tips on how to manage multiple window controllers more effectively would be greatly appreciated! Am I missing something obvious? The stack includes Swift and several other technologies. Any suggestions would be helpful. What am I doing wrong? This is part of a larger CLI tool I'm building.