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

Related searches

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 can view and edit the customer information.

This is an Xcode 8.3.3 project using a storyboard and Swift. It is not a document or core data app.

I have the main window working up to the point where the table view is displaying the records correctly and the associated view controller is receiving the double-click events and logging them to the console.

I have created an additional window controller and view for the edit window and verified its basic functionality by temporarily marking it as the initial controller.

What I haven't been able to figure out is how to display a new instance of that window when the user double-clicks a row.

Thanks to @Joshua Nozzi I'm closer. Here is the code at this point.

let storyboard = NSStoryboard(name: "Main", bundle: nil)
if let windowController = storyboard.instantiateController(withIdentifier: "xyzzy") as? NSWindowController

It's generating a

(Storyboard: 0x620000000680) doesn't contain a controller with identifier 'xyzzy'


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

swift (macos) open new window, xib and AppDelegate.switf I create a new Cocoa class subblass of NSWindowController. This has create new .xib and .swift files which I named them� I'm trying to spin up additional windows upon a button pressed in a simple swift-macos application (no storyboard). So in addition to default MainMenu.xib and AppDelegate.switf I create a new Cocoa class subblass of NSWindowController. This has create new .xib and .swift files which I named them myNewWindow.xib and myNewWindow.swift accordingly.

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.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.

Windows and WindowController Tutorial for macOS, There are some information on Stack Overflow or on other sites but nothing really complete. So, having a lot of knowledge from past how to do the� This tutorial teaches how to open multiple/many view controllers in the same window controller in Xcode with Swift for a MacOS application. Setup: Xcode 11 Platform: MacOS Language: Swift 5 User…

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.

How to open window in MacOS programmatically using Swift |, This tutorial shows you how to open a window from a menu item in cocoa development using Duration: 6:10 Posted: Apr 27, 2014 In the dialog that pops up, select macOS / Source / Cocoa Class and click Next. Create a new class called WindowController and make it a subclass of NSWindowController. Make sure the checkbox to create a XIB is unchecked, and the Language is set to Swift. Click Next and choose the location to save the new file.

Open a New Window From a Menu Item Cocoa, How to control multiple windows in Cocoa.Duration: 16:43 Posted: Aug 3, 2011 I am reading "Cocoa programming for OS X" (The Big Neard Ranch Guid) and I have made a simplest application to test an empty windows app with a window controller and xib file for this controller. I have wrote all necessary code according to the book but after building the app and closing the window of app I cannot reopen the window of the app.

Cocoa Programming L18 - Multiple Windows, I want to show/hide a window in swift by clicking a button from main window. Beginsheet is showing the window, but endsheet is not closing the window. My appdelegate code is given: import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow!

Tag: xcode,osx,swift,cocoa. This may be very trivial question but I am not able to figure it out. I have removed titlebar of a window by-self.window?.styleMask = NSBorderlessWindowMask self.window?.movableByWindowBackground = true Now Any textfield is not working and are disabled because without title bar canBecomeKeyWindow is set to false.

  • I haven't read the Window Programming guide cover-to-cover, but I did spend about an hour in it earlier this morning and did not find an example of how to actually instantiate the new controller and display the window and view. The current window is being automatically displayed by whatever Xcode created when I created my project. Also, I'm new to Swift & Cocoa, but am a very experienced developer in general.
  • Ah. In that case, see the addition to my answer. :-)
  • Cool. I was on the right track hours ago. Question now is where does the window controller identifier come from?
  • Are you sure you set the storyboard identifier for the window controller and not the window? It's actually the third tab and when the controller is selected, it's under Identity, called "Storyboard ID". It's case-sensitive, of course.
  • That was it. Turns out clicking the window controller window in the storyboard selects the window inside the controller. You have to click the window controller "title bar" (where the controller and first responder icons live), instead. Thanks very much for the assist.
  • Instead of a link, please reproduce the answer here.