Sample CodemacOSReviewed 2026-07-21View on Apple Developer

Creating and Customizing the Touch Bar

At a glance

Item Summary
Purpose Demonstrate many NSTouchBar item types, customization choices, scrubbers, and background-window behavior.
Architecture Two parallel implementations—Swift and Objective-C—of the same catalog-style AppKit app.
Main composition A primary table routes to feature-specific storyboard controllers; each detail controller constructs its own touch bar.

Project structure

├── Swift/NSTouchBar Catalog/
│   ├── AppDelegate.swift
│   ├── PrimaryViewController.swift
│   ├── WindowController.swift
│   ├── PhotoManager.swift
│   └── *ViewController.swift
└── Objective-C/NSTouchBar Catalog/
    ├── main.m, AppDelegate.{h,m}
    ├── PrimaryViewController.{h,m}
    └── TestViewControllers/**

This is a broad API catalog, not one production feature graph. The language trees should be read as alternatives, not as layers that call each other.

Overall architecture

Reference code

Swift/NSTouchBar Catalog/PrimaryViewController.swift:63 — catalog data selects a storyboard and replaces the detail item.

guard let arrangedObjects = contentArray.arrangedObjects as? [AnyObject],
    let testCase = arrangedObjects[tableView.selectedRow] as? [String: String],
    let storyboardName = testCase[TestCaseKey.kind]
    else { return }

viewController = NSStoryboard(name: NSStoryboard.Name(storyboardName), bundle: nil)
    .instantiateInitialController() as! NSViewController

Ownership and state

Ownership evidence

Swift/NSTouchBar Catalog/PhotoManager.swift:11 — the photo catalog is one shared owner with a weak observer.

final class PhotoManager {
    // ...
}

Window and feature controllers own their AppKit objects. The primary controller owns the active detail relationship and binds its touchBar to that controller. PhotoManager.shared owns asynchronously loaded thumbnails and reports completion without retaining its delegate.

Class and protocol design

Type group Responsibility
PrimaryViewController Route catalog selection to a detail storyboard.
Feature *ViewController types Construct and react to one touch-bar API example.
WindowController Provide the window-level bar and launch the background window.
PhotoManager / PhotoManagerDelegate Share asynchronously prepared image data with scrubber examples.
Custom *View types Render scrubber items, selections, overlays, or canvas content.

AppKit delegate conformances are commonly placed in Swift extensions; the Objective-C version keeps public declarations in headers and implementation details in .m class extensions.

Access control

Boundary Effect and rationale
Swift private init on PhotoManager Enforces construction through shared, so photo state has one owner.
Swift private helpers and state Restrict controller implementation details while storyboard outlets and actions remain target-visible.
Implicit internal catalog types Keeps all examples inside the app module; no public library is intended.
Objective-C .h interfaces Make controller classes visible to the target and storyboard runtime.
Objective-C class-extension properties Hide outlets and mutable implementation state from headers.

Objective-C/NSTouchBar Catalog/PrimaryViewController.m:10 demonstrates the private class-extension surface for outlets.

Logic ownership and placement

Logic Owner
Catalog routing PrimaryViewController
Window-wide customization opt-in AppDelegate
Item identifiers and item factories The feature controller demonstrating those items
Photo loading and thumbnail cache PhotoManager
Drawing and selection visuals Specialized custom views

Design patterns

Pattern Evidence Purpose
Catalog/router Swift/NSTouchBar Catalog/PrimaryViewController.swift:28 Drives many independent demonstrations from plist metadata.
Delegate factory Swift/NSTouchBar Catalog/ButtonViewController.swift:52 Creates touch-bar items on AppKit request.
Singleton Swift/NSTouchBar Catalog/PhotoManager.swift:16 Shares one image load across examples.
Target/action Swift/NSTouchBar Catalog/ButtonViewController.swift:58 Connects touch-bar controls to controller commands.
Parallel implementations Objective-C/NSTouchBar Catalog/PrimaryViewController.m:28 Shows equivalent architecture in Objective-C rather than adding a runtime dependency.

Naming conventions

  • Controllers are named after the API variant they demonstrate: ButtonViewController, ScrubberViewController, and PopoverViewController.
  • Identifier properties describe the corresponding item (button, buttonBar) and use reverse-DNS raw values.
  • Views use visual-role suffixes such as ItemView, BackgroundView, and OverlayView.

Architecture takeaways

  • A catalog scales by routing metadata to small, isolated feature controllers.
  • Keep touch-bar item construction beside the actions and state it controls.
  • Treat the Swift and Objective-C trees as reference alternatives.
  • The sample favors framework delegates and target/action; only the photo callback is an app-defined protocol.

Source map

Source Role
Swift/NSTouchBar Catalog/PrimaryViewController.swift:49 Feature routing and bar binding
Swift/NSTouchBar Catalog/WindowController.swift:34 Window-level touch bar
Swift/NSTouchBar Catalog/ButtonViewController.swift:23 Representative item factory
Swift/NSTouchBar Catalog/PhotoManager.swift:11 Shared async photo owner
Objective-C/NSTouchBar Catalog/PrimaryViewController.m:62 Objective-C routing counterpart