How do I open another window in macOS in Swift with Cocoa

后端 未结 3 1326
时光说笑
时光说笑 2021-01-11 11:13

I am working on a macOS app that presents a list of customer master records in a table view. Double-clicking a row in the table view should open a new window where the user

相关标签:
3条回答
  • 2021-01-11 11:38

    Additionally, to open new window, this code can help you

    windowController.contentViewController = tabViewController

    The full code is like that i used it in my project:

    @objc func openApplicationView(_ sender: Any?) {
        let mainStoryBoard = NSStoryboard(name: "Main", bundle: nil)
        let tabViewController = mainStoryBoard.instantiateController(withIdentifier: "tabView") as? TabViewController
        let windowController = mainStoryBoard.instantiateController(withIdentifier: "secondWindow") as! TabViewWindowController
        windowController.showWindow(self)
        windowController.contentViewController = tabViewController
    }
    

    It can helpful if you've closed the mainWindow. So you need to add one windowController and tabViewController(you can use normal view controller) in your own underlying storyboard.

    In my side the tabViewController has been extended by NSTabViewController and tab view component has been bound with this class.

    Note: I've also added the windowController in my Main.storyboard as a component and identified to use then on coding side.

    0 讨论(0)
  • 2021-01-11 11:41

    The Window Programming Guide is a great place to understand how windows are managed in general.

    Specifically (assuming you know how to present a window controller scene in storyboards), you need somewhere to store references to the new window controllers so they’re not immediately deallocated (and disappear) when presented.

    In your case, you may want to keep an array of your open detail windows in the master window controller, so that if the master goes away, the details do as well. When a detail window is open (a controller instance is created and its window shown), you’ll store its controller in the array; when closed, you remove its controller from the array so it’s deallocated.

    There are a number of ways to do this, depending on how much control you want, how you want child window ownership to work, etc., but this basic pattern is usually sufficient.

    To instantiate a new window controller scene from a storyboard:

    var myWindowController = NSStoryboard(name: "MyStoryboardFileName", bundle: nil)?.instantiateControllerWithIdentifier("MyWindowControllerIdentifier") as MyWindowControllerClass
    myWindowController?.showWindow(self)
    
    0 讨论(0)
  • Confirmed. This is such a major gotcha in the UI. And it is buggy to boot: Storyboard doesn't contain a controller with identifier 'MainWindow' A whole day wasted troubleshooting this issue.

    0 讨论(0)
提交回复
热议问题