<!--
{
  "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:bundle:)`](/documentation/AppKit/NSViewController/init(nibName:bundle:))

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

[`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

[`representedObject`](/documentation/AppKit/NSViewController/representedObject)

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

### Nib Properties

[`nibBundle`](/documentation/AppKit/NSViewController/nibBundle)

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

[`nibName`](/documentation/AppKit/NSViewController/nibName)

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

### View Properties

[`view`](/documentation/AppKit/NSViewController/view)

The view controller’s primary view.

[`title`](/documentation/AppKit/NSViewController/title)

The localized title of the receiver’s primary view.

### View Property Wrappers

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

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

### NSEditor Conformance

[`commitEditing(withDelegate:didCommit:contextInfo:)`](/documentation/AppKit/NSViewController/commitEditing(withDelegate:didCommit:contextInfo:))

Attempt to commit any currently edited results of the receiver.

[`commitEditing()`](/documentation/AppKit/NSViewController/commitEditing())

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

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

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

### Using a Storyboard

[`storyboard`](/documentation/AppKit/NSViewController/storyboard)

The storyboard from which the view controller was loaded.

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

### Responding to View Events

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

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

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

[`isViewLoaded`](/documentation/AppKit/NSViewController/isViewLoaded)

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

[`viewIfLoaded`](/documentation/AppKit/NSViewController/viewIfLoaded)

[`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.

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

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

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

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

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

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

### Managing View Layout

[`preferredContentSize`](/documentation/AppKit/NSViewController/preferredContentSize)

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

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

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

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

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

[`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

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

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

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

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

[`transition(from:to:options:completionHandler:)`](/documentation/AppKit/NSViewController/transition(from:to:options:completionHandler:))

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

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

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

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

Removes a specified child controller from the view controller.

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

Removes the called view controller from its parent view controller.

[`preferredContentSizeDidChange(for:)`](/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

[`present(_:animator:)`](/documentation/AppKit/NSViewController/present(_:animator:))

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

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

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

[`present(_:asPopoverRelativeTo:of:preferredEdge:behavior:)`](/documentation/AppKit/NSViewController/present(_:asPopoverRelativeTo:of:preferredEdge:behavior:))

Presents another view controller as a popover.

[`present(_:asPopoverRelativeTo:of:preferredEdge:behavior:hasFullSizeContent:)`](/documentation/AppKit/NSViewController/present(_:asPopoverRelativeTo:of:preferredEdge:behavior:hasFullSizeContent:))

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

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

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

Presents another view controller as a sheet.

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

### Getting Related View Controllers

[`parent`](/documentation/AppKit/NSViewController/parent)

The immediate ancestor view controller of the view controller.

[`presentedViewControllers`](/documentation/AppKit/NSViewController/presentedViewControllers)

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

[`presentingViewController`](/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

[`extensionContext`](/documentation/AppKit/NSViewController/extensionContext)

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

[`preferredScreenOrigin`](/documentation/AppKit/NSViewController/preferredScreenOrigin)

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

[`preferredMaximumSize`](/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.

[`preferredMinimumSize`](/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.

[`viewWillTransition(to:)`](/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.

[`sourceItemView`](/documentation/AppKit/NSViewController/sourceItemView)

### Constants

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

Animation options for view transitions in a view controller.

### Initializers

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



---

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)