Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
{
  windowController.showWindow(self)
}

It's generating a

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

error.

like image 270
David Patterson Avatar asked Jul 15 '17 15:07

David Patterson


People also ask

How do I open windows in Swift?

Step 1: Write a basic program in Swift with your favorite editor. Step 2: Open "Swift for Windows 1.6" and click 'Select File' to choose your file. Step 3: Click 'Compile' to compile your program. Step 4: Click 'Run' to run on Windows.

How do I open multiple Xcode windows?

Use File/Open ( ⌘O ) to open a project in another window. Show activity on this post. Firstly, be sure to un-maximize the window by hovering on top left and touching the green button (if presently maximized). If you use Command+T then afterwards you can drag that tab into the desktop space to make a new window.

How do I create a new window in Xcode?

The Xcode project window is your primary interface for viewing, editing, and managing all parts of your project. You can configure it to fit your work style and adjust it as you work on different tasks. The main window opens when you create or open a project. To open additional main windows, choose File > New > Window.

What is NSWindowController?

An NSWindowController (NSWC) subclass exists (conceptually) just beneath every window nib, acting as the glue between the user interface elements and the model objects that they control/represent. Basically, every window in your application should have its own NSWC subclass.


1 Answers

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)
like image 131
Joshua Nozzi Avatar answered Oct 16 '22 04:10

Joshua Nozzi