<!--
{
  "availability" : [
    "macOS: 10.5.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSViewController",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSViewController"
  },
  "title" : "NSViewController"
}
-->

# NSViewController

A controller that manages a view, typically loaded from a nib file.

```
class NSViewController
```

## Overview

View controller management includes:

- Memory management of top-level objects similar to that performed by the [`NSWindowController`](/documentation/AppKit/NSWindowController) class, taking the same care to prevent reference cycles when controls are bound to the nib file’s owner.
- Declaring a generic [`view`](/documentation/AppKit/NSViewController/view) property, to make it easy to establish bindings in the nib to an object that isn’t yet known at nib-loading time or readily available to the code that’s doing the nib loading.
- Implementing the key-value binding NSEditor informal protocol, so that apps using a view controller can easily make bound controls in the views commit or discard changes by the user.

In macOS 10.10 and later, a view controller offers a full set of life cycle methods, allowing you to manage the content of a window in a way that is on a par with iOS view controller management. These methods, presented in order here to reflect a typical cycle, are:

*View life cycle:*

1. [`viewDidLoad()`](/documentation/AppKit/NSViewController/viewDidLoad())
2. [`viewWillAppear()`](/documentation/AppKit/NSViewController/viewWillAppear())
3. [`viewDidAppear()`](/documentation/AppKit/NSViewController/viewDidAppear())

*User interaction cycle:*

1. [`updateViewConstraints()`](/documentation/AppKit/NSViewController/updateViewConstraints())
2. [`viewWillLayout()`](/documentation/AppKit/NSViewController/viewWillLayout())
3. [`viewDidLayout()`](/documentation/AppKit/NSViewController/viewDidLayout())
4. [`viewWillDisappear()`](/documentation/AppKit/NSViewController/viewWillDisappear())
5. [`viewDidDisappear()`](/documentation/AppKit/NSViewController/viewDidDisappear())

In addition, in macOS 10.10 and later, a view controller participates in the responder chain. You can implement action methods directly in the view controller. Corresponding actions that originate in the view controller’s view proceed up the responder chain and are handled by those methods.

Prior to OS X v10.10, a typical usage pattern for loading a nib file was to subclass [`NSViewController`](/documentation/AppKit/NSViewController) and override its [`loadView()`](/documentation/AppKit/NSViewController/loadView()) method to call `[super loadView]`. But in macOS 10.10 and later, the [`loadView()`](/documentation/AppKit/NSViewController/loadView()) method automatically looks for a nib file with the same name as the view controller. To take advantage of this behavior, name a nib file after its corresponding view controller and pass `nil` to both parameters of the [`init(nibName:bundle:)`](/documentation/AppKit/NSViewController/init(nibName:bundle:)) method.

A view controller employs lazy loading of its view: Immediately after a view controller is loaded into memory, the value of its [`isViewLoaded`](/documentation/AppKit/NSViewController/isViewLoaded) property is <doc://com.apple.documentation/documentation/Swift/false>. The value changes to <doc://com.apple.documentation/documentation/Swift/true> after the [`loadView()`](/documentation/AppKit/NSViewController/loadView()) method returns and just before the system calls the [`viewDidLoad()`](/documentation/AppKit/NSViewController/viewDidLoad()) method.

A view controller is meant to be highly reusable, such as for dynamically representing various objects. For example, the  [`addAccessoryController(_:)`](/documentation/AppKit/NSPageLayout/addAccessoryController(_:)) methods of the [`NSPageLayout`](/documentation/AppKit/NSPageLayout) and [`NSPrintPanel`](/documentation/AppKit/NSPrintPanel) classes take an [`NSViewController`](/documentation/AppKit/NSViewController) instance as the argument, and set the [`representedObject`](/documentation/AppKit/NSViewController/representedObject) property to the [`NSPrintInfo`](/documentation/AppKit/NSPrintInfo) object that is to be shown to the user. This allows a developer to easily create new printing accessory views using bindings and the [`NSPrintInfo`](/documentation/AppKit/NSPrintInfo) class’s key-value coding and key-value observing compliance. When the user dismisses a printing dialog, the  [`NSPageLayout`](/documentation/AppKit/NSPageLayout) and [`NSPrintPanel`](/documentation/AppKit/NSPrintPanel) classes each send NSEditor messages to each accessory view controller to ensure that the user’s changes have been committed or discarded properly. The titles of the accessories are retrieved from the view controllers and shown to the user in menus that the user can choose from.

## Topics

### Creating A View Controller

[`init(nibName: NSNib.Name?, bundle: Bundle?)`](/documentation/AppKit/NSViewController/init(nibName:bundle:))

Returns a view controller object initialized to the nib file in the specified bundle.

[`func loadView()`](/documentation/AppKit/NSViewController/loadView())

Instantiates a view from a nib file and sets the value of the [`view`](/documentation/AppKit/NSViewController/view) property.

### Represented Object

[`var representedObject: Any?`](/documentation/AppKit/NSViewController/representedObject)

The object whose value is presented in the receiver’s primary view.

### Nib Properties

[`var nibBundle: Bundle?`](/documentation/AppKit/NSViewController/nibBundle)

The nib bundle to be loaded to instantiate the receiver’s primary view.

[`var nibName: NSNib.Name?`](/documentation/AppKit/NSViewController/nibName)

The name of the nib file to be loaded to instantiate the receiver’s primary view.

### View Properties

[`var view: NSView`](/documentation/AppKit/NSViewController/view)

The view controller’s primary view.

[`var title: String?`](/documentation/AppKit/NSViewController/title)

The localized title of the receiver’s primary view.

### View Property Wrappers

[`struct ViewLoading`](/documentation/AppKit/NSViewController/ViewLoading)

A property wrapper that loads the view controller’s view before accessing the property.

### NSEditor Conformance

[`func commitEditing(withDelegate: Any?, didCommit: Selector?, contextInfo: UnsafeMutableRawPointer?)`](/documentation/AppKit/NSViewController/commitEditing(withDelegate:didCommit:contextInfo:))

Attempt to commit any currently edited results of the receiver.

[`func commitEditing() -> Bool`](/documentation/AppKit/NSViewController/commitEditing())

Returns whether the receiver was able to commit any pending edits.

[`func discardEditing()`](/documentation/AppKit/NSViewController/discardEditing())

Causes the receiver to discard any changes, restoring the previous values.

### Using a Storyboard

[`var storyboard: NSStoryboard?`](/documentation/AppKit/NSViewController/storyboard)

The storyboard from which the view controller was loaded.

[`func dismiss(Any?)`](/documentation/AppKit/NSViewController/dismiss(_:)-3n76y)

### Responding to View Events

[`func viewDidLoad()`](/documentation/AppKit/NSViewController/viewDidLoad())

Called after the view controller’s view has been loaded into memory.

[`func loadViewIfNeeded()`](/documentation/AppKit/NSViewController/loadViewIfNeeded())

[`var isViewLoaded: Bool`](/documentation/AppKit/NSViewController/isViewLoaded)

A Boolean value indicating whether the view controller’s view is loaded into memory.

[`var viewIfLoaded: NSView?`](/documentation/AppKit/NSViewController/viewIfLoaded)

[`func viewWillAppear()`](/documentation/AppKit/NSViewController/viewWillAppear())

Called after the view controller’s view has been loaded into memory is about to be added to the view hierarchy in the window.

[`func viewDidAppear()`](/documentation/AppKit/NSViewController/viewDidAppear())

Called when the view controller’s view is fully transitioned onto the screen.

[`func viewWillDisappear()`](/documentation/AppKit/NSViewController/viewWillDisappear())

Called when the view controller’s view is about to be removed from the view hierarchy in the window.

[`func viewDidDisappear()`](/documentation/AppKit/NSViewController/viewDidDisappear())

Called after the view controller’s view is removed from the view hierarchy in a window.

### Managing View Layout

[`var preferredContentSize: NSSize`](/documentation/AppKit/NSViewController/preferredContentSize)

The desired size of the view controller’s view, in screen units.

[`func updateViewConstraints()`](/documentation/AppKit/NSViewController/updateViewConstraints())

Called during Auto Layout constraint updating to enable the view controller to mediate the process.

[`func viewWillLayout()`](/documentation/AppKit/NSViewController/viewWillLayout())

Called just before the [`layout()`](/documentation/AppKit/NSView/layout()) method of the view controller’s view is called.

[`func viewDidLayout()`](/documentation/AppKit/NSViewController/viewDidLayout())

Called immediately after the [`layout()`](/documentation/AppKit/NSView/layout()) method of the view controller’s view is called.

### Managing Child View Controllers in a Custom Container

[`func addChild(NSViewController)`](/documentation/AppKit/NSViewController/addChild(_:))

A convenience method for adding a child view controller at the end of the [`children`](/documentation/AppKit/NSViewController/children) array.

[`var children: [NSViewController]`](/documentation/AppKit/NSViewController/children)

An array of view controllers that are hierarchical children of the view controller.

[`func transition(from: NSViewController, to: NSViewController, options: NSViewController.TransitionOptions, completionHandler: (() -> Void)?)`](/documentation/AppKit/NSViewController/transition(from:to:options:completionHandler:))

Performs a transition between two sibling child view controllers of the view controller.

[`func insertChild(NSViewController, at: Int)`](/documentation/AppKit/NSViewController/insertChild(_:at:))

Inserts a specified child view controller into the [`children`](/documentation/AppKit/NSViewController/children) array at a specified position.

[`func removeChild(at: Int)`](/documentation/AppKit/NSViewController/removeChild(at:))

Removes a specified child controller from the view controller.

[`func removeFromParent()`](/documentation/AppKit/NSViewController/removeFromParent())

Removes the called view controller from its parent view controller.

[`func preferredContentSizeDidChange(for: NSViewController)`](/documentation/AppKit/NSViewController/preferredContentSizeDidChange(for:))

Called when there is a change in value of the [`preferredContentSize`](/documentation/AppKit/NSViewController/preferredContentSize) property of a child view controller or a presented view controller.

### Presenting Another View Controller’s Content

[`func present(NSViewController, animator: any NSViewControllerPresentationAnimator)`](/documentation/AppKit/NSViewController/present(_:animator:))

Presents another view controller using a specified, custom animator for presentation and dismissal.

[`func dismiss(NSViewController)`](/documentation/AppKit/NSViewController/dismiss(_:)-91my5)

Dismisses a presented view controller, using the same animator that presented it.

[`func present(NSViewController, asPopoverRelativeTo: NSRect, of: NSView, preferredEdge: NSRectEdge, behavior: NSPopover.Behavior)`](/documentation/AppKit/NSViewController/present(_:asPopoverRelativeTo:of:preferredEdge:behavior:))

Presents another view controller as a popover.

[`func present(NSViewController, asPopoverRelativeTo: NSRect, of: NSView, preferredEdge: NSRectEdge, behavior: NSPopover.Behavior, hasFullSizeContent: Bool)`](/documentation/AppKit/NSViewController/present(_:asPopoverRelativeTo:of:preferredEdge:behavior:hasFullSizeContent:))

[`func presentAsModalWindow(NSViewController)`](/documentation/AppKit/NSViewController/presentAsModalWindow(_:))

Presents another view controller as a modal window, also known as an alert.

[`func presentAsSheet(NSViewController)`](/documentation/AppKit/NSViewController/presentAsSheet(_:))

Presents another view controller as a sheet.

[`func present(inWidget: NSViewController)`](/documentation/AppKit/NSViewController/present(inWidget:))

### Getting Related View Controllers

[`var parent: NSViewController?`](/documentation/AppKit/NSViewController/parent)

The immediate ancestor view controller of the view controller.

[`var presentedViewControllers: [NSViewController]?`](/documentation/AppKit/NSViewController/presentedViewControllers)

The view controllers, if any, that are currently presented by the view controller.

[`var presentingViewController: NSViewController?`](/documentation/AppKit/NSViewController/presentingViewController)

The view controller that presented the view controller or that presented its farthest ancestor view controller.

### Configuring an App Extension View Controller

[`var extensionContext: NSExtensionContext?`](/documentation/AppKit/NSViewController/extensionContext)

For a view controller that is part of an app extension, the app extension context.

[`var preferredScreenOrigin: NSPoint`](/documentation/AppKit/NSViewController/preferredScreenOrigin)

For a view controller that is part of an app extension, the preferred screen origin.

[`var preferredMaximumSize: NSSize`](/documentation/AppKit/NSViewController/preferredMaximumSize)

For a view controller that is part of an app extension, the largest allowable size for the app extension’s primary view, in screen units.

[`var preferredMinimumSize: NSSize`](/documentation/AppKit/NSViewController/preferredMinimumSize)

For a view controller that is part of an app extension, the smallest allowable size for the app extension’s primary view, in screen units.

[`func viewWillTransition(to: NSSize)`](/documentation/AppKit/NSViewController/viewWillTransition(to:))

For a view controller that is part of an app extension, called when its view is about to be resized.

[`var sourceItemView: NSView?`](/documentation/AppKit/NSViewController/sourceItemView)

### Constants

[`struct TransitionOptions`](/documentation/AppKit/NSViewController/TransitionOptions)

Animation options for view transitions in a view controller.

### Initializers

[`init?(coder: NSCoder)`](/documentation/AppKit/NSViewController/init(coder:))

## Relationships

### Inherited By

[`NSTabViewController`](/documentation/AppKit/NSTabViewController)

[`NSCollectionViewItem`](/documentation/AppKit/NSCollectionViewItem)

[`NSSplitViewItemAccessoryViewController`](/documentation/AppKit/NSSplitViewItemAccessoryViewController)

[`NSTitlebarAccessoryViewController`](/documentation/AppKit/NSTitlebarAccessoryViewController)

[`NSSplitViewController`](/documentation/AppKit/NSSplitViewController)

[`NSPageController`](/documentation/AppKit/NSPageController)

### Conforms To

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSExtensionRequestHandling`](/documentation/Foundation/NSExtensionRequestHandling)

[`NSUserInterfaceItemIdentification`](/documentation/AppKit/NSUserInterfaceItemIdentification)

[`Sendable`](/documentation/Swift/Sendable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Equatable`](/documentation/Swift/Equatable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSSeguePerforming`](/documentation/AppKit/NSSeguePerforming)

[`NSEditor`](/documentation/AppKit/NSEditor)

[`Hashable`](/documentation/Swift/Hashable)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`Copyable`](/documentation/Swift/Copyable)

[`NSTouchBarProvider`](/documentation/AppKit/NSTouchBarProvider)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Escapable`](/documentation/Swift/Escapable)

[`playgroundliveviewable`](/documentation/playgroundsupport/playgroundliveviewable)

[`NSUserActivityRestoring`](/documentation/AppKit/NSUserActivityRestoring)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSStandardKeyBindingResponding`](/documentation/AppKit/NSStandardKeyBindingResponding)

### Inherits From

[`NSResponder`](/documentation/AppKit/NSResponder)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)