Add a window controller class of the Cocoa Hello World | How To Create macOS Apps

This is a series of of articles explaining how to create Cocoa version of Hello World. In this article, we create a window class and make it a window controller of the Hello World window.

This article uses a sample code which was created in the previous article. If you don’t have it, download from the previous article.

TOC

What is a window controller?

AppKit uses a window and its window controller to control windows which is displayed in the app. Open the Main.storyboard file.

Main.storyboard
Main.storyboard

Notice the area circled in the screen capture. It contains the following three scenes.

  • Application Scene
  • Window Controller Scene
  • View Controller Scene

A scene which is explained in this article is Window Controller Scene. You can find the Window Controller if you open the disclosure button of the Window Controller Scene. Open the disclosure button of the Window Controller, you will find the Window.

As above, the Window is managed by the Window Controller and the Window Controller is managed by the Window Controller Scene.

Select the Window Controller Scene and see the attributes inspector. The “Is Initial Controller” option is on. The Window Controller Scene which “Is Initial Controller” option is on will be used first when its storyboard file is loaded.

When the Main.storyboard is loaded, the Window Controller scene is used and the window controller which is managed by the scene is loaded, the window is managed by the Window Controller is displayed.

Add the window controller

In AppKit, the window controller class is NSWindowController class. When the window is something operated or its state is changed, the AppKit uses call methods of NSWindowController or delegate of NSWindowController.

When you create the AppKit app, you create the sub class of the NSWindowController class to implement your own window controller class.

Lets implement a window controller class for the Cocoa Hello World.

Add a class

Add a class to project. Do as follows.

STEP
Select “HelloWorld” group in the project navigator in the project window.
STEP
Select “File…” from the “New” in the “File” menu.
STEP
Select the “Cocoa Class” of the “macOS” category and click the “Next” button.

You can also select the “Swift File”. The difference is only generated code.

File templates
File templates
STEP
Enter the new file options.

Enter as follows and click the “Next” button.

Option Description
Class HelloWindowController
Subclass of NSWindowController
Also create XIB file for user interface Off
Language Swift
Options for the new file
STEP
The save panel is displayed. Click the “Create” button with leaving default values.
Select the directory
Select the directory
STEP
The “HelloWindowController” class which is a sub class of the NSWindowController class is created.
import Cocoa

class HelloWindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()
    
        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    }

}

Change the class of the instance

When the Main.storyboard is loaded and the Window Controller Scene is displayed, AppKit instantiate the NSWindowController class.

Change the the class to the HelloWindowController class from the NSWindowController class. Do following.

STEP
Open the “Main.storyboard”.
STEP
Select the “Window Controller” in the “Window Controller Scene”.
STEP
Show the Identity inspector.
An identity of the window controller
An identity of the window controller
STEP
Change the class to the “HelloWindowController” in the identity inspector.
Change the class
Change the class

Now , the instantiation class is changed to the HelloWindowController class.

Download the sample code

The sample code was created in this article can be downloaded from here.

List of Serialized Articles

This article is part of a series of articles titled “How to create macOS Apps with AppKit”. For other articles, please open the following links.

How to create macOS apps with AppKit List of Serialized Articles

Let's share this post !

Author of this article

Akira Hayashiのアバター Akira Hayashi Representative, Software Engineer

I am an application developer loves programming. This blog is a tech blog, its articles are learning notes. In my work, I mainly focus on desktop and mobile application development, but I also write technical books and teach seminars. The websites of my work and books are here -> RK Kaihatsu.

TOC