I'm trying to programmatically launch a window from AppDelegate, specifically from Services Provider and it works but not very well because the toolbar doesn't work, Touch Bar doesn't show up and custom menu items I've added are disabled. The default menus and menu items provided by Apple works.
All my views are created on storyboards. The default main window is not launched from AppDelegate.
I launch the window like this.
guard let windowController = NSStoryboard.main?.instantiateController(withIdentifier: .windowController) as? WindowController else { return }
windowController.window?.title = data.title
windowController.viewController?.property = data
windowController.showWindow(self)
However, if I initiate by segue, it works perfectly.
guard let splitViewController = NSApp.mainWindow?.contentViewController as? NSSplitViewController,
let viewController = splitViewController.splitViewItems[1].viewController as? ViewController else { return }
viewController.anotherViewController.performSegue(withIdentifier: .segue, sender: data)
So, why don't I do that? Because it works from the main window and then that programmatic launched window prevent that code from working again. It only works when there is only one window.
I want it to work every single time. It doesn't matter if the main window is the key window or the subsequent other non-main windows.
How can I make sure that I can launch a window and it is also recognised by the toolbar, Touch Bar and the custom menu items and it doesn't matter if there is only the main window or other windows which are not the main window but different type of windows.
I misunderstood the mainWindow
property, thinking it refer to the actual main window when it really point to the focussed window. So, the main window that I actually want is the first window from the windows
array.
guard let mainWindowController = NSApp.windows.first?.windowController as? MainWindowController,
let splitViewController = mainWindowController.contentViewController as? SplitViewController,
let viewController = splitViewController.splitViewItems[1].viewController as? ViewController else { return }
viewController.anotherViewController.performSegue(withIdentifier: .segueIdentifier, sender: data)
The menu items, toolbar and Touch Bar are working and responding to user interactions.