Terminate the app when the window is closed | How to create macOS Apps

This article explains that how to terminate the app when the window is closed.

We add the code to the Cocoa Hello World which was created in the previous article. You can download it from the following article.

TOC

About the delegate

AppKit uses the delegate pattern to execute app-side processing from the AppKit. The English word “delegate” means “delegate” or “representative,” but in app development using AppKit, it means to take over a part of the processing. You can imagine callback objects or callback functions.

In Swift and Objective-C, the delegate is defined by the protocol, and the delegate object is an instance of a conforming class of the protocol that implements the methods defined by the protocol.

If you try to define it in words, it becomes complicated, but I think you will be able to get some idea of what it is like when you use it.

NSWindowDelegate

AppKit executes the methods of the window’s delegate when the window is closed. The methods that are executed are the following methods of the NSWindowDelegate protocol.

- (void)windowWillClose:(NSNotification *)notification;

Implement the delegate class

Implement the class that is conforming to NSWindowDelegate protocol. Because the Cocoa Hello World is small app, implement the delegate methods in the HelloWindowController class. Make the HelloWindowController class doubles as a window controller and delegate. It is easy to understand window related controls are all in one place.

Add the delegate code

Add the code to HelloWindowController.swift as follows.

import Cocoa

class HelloWindowController: NSWindowController, NSWindowDelegate {
    
    let minWindowWidth: CGFloat = 200
    let minWindowHeight: CGFloat = 150
    let maxWindowWidth: CGFloat = 1200
    let maxWindowHeight: CGFloat = 900

    override func windowDidLoad() {
        super.windowDidLoad()
        
        window?.minSize = CGSize(width: minWindowWidth,
                                 height: minWindowHeight)
        window?.maxSize = CGSize(width: maxWindowWidth,
                                 height: maxWindowHeight)

        window?.makeKeyAndOrderFront(nil)
        window?.center()        
    }

    func windowWillClose(_ notification: Notification) {
        NSApplication.shared.terminate(self)
    }
}

Changed parts are following two parts.

  • Add the NSWindowDelegate declaration.
  • Add the windowWillClose() method.

Terminate the app

To terminate the app in AppKit app, use terminate() method of the NSApplication class.

An instance of the NSApplication class is created for each processes, you can get it with shared property of the NSApplication class, and you use it commonly in you app.

Set the delegate of the window

To set the delegate of the window, configure in the storyboard file or write the code.

By a code

Set an instance of the delegate object to the delegate property of the NSWindow class.

Configure in the storyboard file

Do following to configure the delegate in the storyboard file. However, the Cocoa Hello World already configured the window controller as a delegate, but it may be different depending on the Xcode versions, so check it.

STEP
Open the “Main.storyboard”.
STEP
Select the “Window” of “Window Controller” of “Window Controller Scene”.
STEP
Open the connection inspector, and check the object which is connected to the “delegate” of the “Outlets”.
Check the connected object
Check the connected object
STEP
Connect Window Controller to the delegate.

If it is out of place, or if something other than Window Controller is set, connect Window Controller to the delegate by referring to the following screen capture. Drag and drop from (1) to (2) in the screen capture and connect the blue line extending from (1).

Properties such as delegate that can be set on the Storyboard are called “outlets”.

Connect the window controller to the delegate
Connect the window controller to the delegate

Test

Run the application. When the window appears, click the window’s close box to close it. The application will then close. Closing the window from the menu by selecting “Close” from the “File” menu will also close the window.

Download the sample code

The sample code 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