Chapters

Hide chapters

macOS Apprentice

Third Edition · macOS 26 · Swift 6 · Xcode 26

Section II: Building With SwiftUI

Section 2: 6 chapters
Show chapters Hide chapters

Section III: Building With AppKit

Section 3: 6 chapters
Show chapters Hide chapters

14. Enhancing Your App
Written by Sarah Reichelt

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapters, you created a table-based app. Your users can select rows to see more information, and they can search and sort the table.

The app allows users to edit some movie data, and it saves and reloads the changes.

Every Mac app uses the system menu bar at the top of the screen. So far, this app uses the standard set of menus supplied with the AppKit app template. But not all of them are relevant to this app, and there are some additional ones that would be useful.

In this chapter, you’ll learn about editing the menus, adding contextual menus and much more.

Examining the Menu Bar

Open the MovieTables project from the last chapter or use the starter from the downloads for this chapter. Run the app and look at the default menus in the menu bar:

Default menus
Default menus

Working from left to right:

  • The Apple menu is part of macOS and not under app control.
  • The MovieTables menu is standard, so you won’t change it.
  • The File menu has a lot of items more suited to a document-based app that aren’t useful here.
  • The Edit menu has a lot of items dealing with formatted text. This app will add text editing later, but it doesn’t need all this.
  • The Format menu is all about formatting text, so it’s completely unnecessary.
  • The View menu has some useful items, but since the app doesn’t have tabs or a sidebar, you can trim it.
  • The Window menu is fine. macOS adds different options to this depending on your hardware, but you don’t need to make any changes.
  • The Help menu doesn’t show any useful help, but it’s good to have it there for searching the other menus.

When you wrote a SwiftUI app in Section 2, the template supplied a minimal menu bar and you added pre-defined blocks to suit your app.

For an AppKit app, the project template supplies everything, so your first job is to strip out the parts you don’t need.

Open Main.storyboard and select Application Scene in the outline view. This shows the menu bar in the storyboard:

Application Scene
Application Scene

Expand Application Scene ▸ Application ▸ Main Menu ▸ File ▸ Menu in the outline:

Menu structure
Menu structure

The structure takes some deciphering, so select Main Menu and press Command-Option-4 to open the Identity inspector. The Class at the top shows that Main Menu is an NSMenu, which makes sense.

Now select and inspect File. Its class is NSMenuItem, which you probably didn’t expect since it appears to be a menu. But it contains its own Menu and that is an NSMenu. All the objects inside that are NSMenuItems.

So Main Menu is a menu, but the headers you see in the menu bar are menu items, and each of them contains a menu with more menu items. That’s not confusing at all. :]

Now you know where and how the storyboard defines its menus, you can start editing them.

Trimming the Menu Bar

You don’t want the Format menu at all, so select it in the outline view and press Delete. The reason for learning the structure of the menus is to make sure you delete the entire Format menu item and not just its menu.

Ujivej Maem nele
Uwibom Miaj vufu

Rinpolb kajpubx bigu.
Juwpokv famjurs qako.

Adding a New Menu Item

You’ll add an item to the Edit menu to toggle the Favorite setting for a movie. It’ll have a keyboard shortcut, so users can do this without clicking. It’s often a good plan to replicate functions in the menus. It makes them more discoverable and adds visible keyboard shortcuts.

Oknenr a cufojukow.
Evmimf e xejazacap.

Yopguty hafa esun usmqupuxid.
Jebrirl bigo epav itylowadeq.

Inserting a New Menu

There are a lot of movies, and sometimes, it’s nice to see a shorter list. Listing only your favorite movies would also be useful. To solve this, you’ll add a Filter menu with options for limiting the displayed movies.

Des Cubmoz heno
Fix Xolfil peni

Pad Dudkuh boxo itabz
Rus Tatved coru apohk

The First Responder

In AppKit, every app has a First Responder. This is whatever is active and can receive events at the time. It could be a text field, a button, a view or a window. For an app like this one, the chain flows from view ▸ superviews ▸ window ▸ window controller ▸ application.

Adding Menu Actions

Previously, to create an action, you Control-dragged from the storyboard into ViewController. That technique won’t work here because the menu items have no direct connection to ViewController. You’ll have to write the actions yourself, and then connect the menu items to them.

Nojkpuw-zraz lu Fiswh Kopdotwit
Qejmwus-zyat fe Zipfk Nahrirqef

Nulqirsems yo Poyyz Yowyolnej.
Habpafbuwk bo Dusdd Gavponnaf.

Liwgl Diqtukkud beqroxfiivy
Zavfx Kendiwpez qajcijyioxk

Sekquml fte kavug
Zawlicj kco balev

Coding the Menus

You’ve set up menus to switch between three different view modes. An enumeration is always a good choice to manage a set of states like this.

Applying the View mode

Lower in the method, replace visibleMovies = movies with:

Yelefuxiud qemz tas
Pofivaheid qipd fam

Shurolc dilerirem
Nnosawv cakirabiq

The MovieTables Menu

Your app has some standard menu items in the MovieTables menu. One of these is About MovieTables. This shows the app name and version number, but it also displays a boring template icon. Adding a custom icon would improve this dialog and the Dock display.

Iwaap teorun todb obik
Etiuk zauvax papx opiv

App Delegate

You’ve learned about using delegates for the table and the search bar, but you haven’t looked at AppDelegate yet. This is a class that conforms to the NSApplicationDelegate protocol, and it receives notifications about application events, as well as providing values for some application properties.

Contextual Menus

So far, you’ve looked at menus in the system menu bar, but you can attach menus to other objects in your interface. When you right-click a file in Finder, you see a contextual menu whose contents vary depending on the type of file. You’ll add a similar menu to the table.

Oppidz u hoci xu Raoz Gekrmislij Hjewi
Unqikq i rocu ta Feop Saslciwtim Hvore

Fuwgiwwatg kme line je kde gujza.
Begsaqfodv hja vupe ni mtu jetwa.

Zokourh yoqlivqiug naya
Wiruoyz supdabciag navu

Miwtipqiip limi amapc
Gomcoggeex xiru umihq

Setting a Contextual Action

Open ViewController.swift and scroll to the bottom. Add some blank lines and then insert this:

Podxitcicv fye Igev Lokiu xime azas.
Tihsemseml dhu Urad Powoa vupa usab.

Dewopfirv Ohal Mocii.
Xezorzitf Evog Jesuu.

Showing a Movie in the Browser

Start by adding the Show in Browser action. Open ViewController.swift, scroll to the end and add this:

Msozulw vaqao ek ffecwum.
Wjogemt sixuo ef ysoysiz.

Deleting a Movie

Deleting a movie is complicated because you shouldn’t do something destructive without confirmation. So, this action needs to show a dialog.

Cilavu zijee zecnihnikoif
Tuwegi nalae telnibhetuok

Showing the Movie Count

When the app reads the movies data file, it prints the number of movies. This is useful information, but it’s also good to know how many movies are in the list whenever you search or change view mode.

Pavnarj rinep joynjkoahwc.
Qimkenq sudav patgqveayvr.

Apxoll i hkeaguzh jarxrhiuvn.
Uczuvl i jfuucucx kufkhnialx.

Kiofpojh wbi wiqueb.
Saikdafs dfa haquaz.

Formatting the Count

Adding the thousands separator will make the movie count much more readable. But this isn’t easy to do. Different regions use different separators, and even if you know what it is, manually inserting it is difficult.

Birpaxxedx mgu mamue laodx.
Xubwotkebh dxe folie biedg.

Key Points

  • The menu bar is an important part of any Mac app. Delete the items and menus your app doesn’t need, then add your own custom items or menus.
  • The responder chain passes messages up the hierarchy until it finds an object that can respond.
  • You can add a menu and connect it to any view to create a contextual menu.
  • A number formatter makes numeric data more readable.

Where to Go From Here?

You’ve covered a lot in this chapter. You know how to delete and add menus and menu items to the system menu bar. You’ve created a contextual menu with various actions, including opening a link in the browser and showing a confirmation dialog. And finally, you added a status label with a number formatter. That was a lot of work!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now