Open a new Window from Window in AppKit

Hi everybody,

As you know, I’m developing a native MacOS App with AppKit. This time I need to know how to open a new Window from a window and close the first window.

let me explain:

if you open Xcode you land on welcome screen. when you click on a button or cell of project, you close the welcome screen and open a new window. so, How can I do this?
Thank you

p.s. the creation of document have to be happen with the creation of new window, exactly like in Xcode. in Apple documentation there is this code:

override func makeWindowControllers() {
    // Returns the storyboard that contains your document window.
    let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
    if let windowController =
        storyboard.instantiateController(
            withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as? NSWindowController {
        addWindowController(windowController)
        
        // Set the view controller's represented object as your document.
        if let contentVC = windowController.contentViewController as? ViewController {
            contentVC.representedObject = content
            contentViewController = contentVC
        }
    }
}

this code create the document with main windowviewcontroller. that means that in my case here I have to pass the content to HomeViewController that is the view controller that I want to open from the main view controller?

@robertomachorro @sarah

Not sure I quite understand the question here.

The example code you posted creates a new view controller from the storyboard. You can set any properties on this new view controller here. This example is setting its representedObject but you may have other properties to set.

You can open this window using the window controller’s showWindow() method.

Closing a window can be done if you have its view controller. I use view.window?.close().

Does that help?

This topic was automatically closed after 166 days. New replies are no longer allowed.