Class

NSViewController

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

Declaration

@interface NSViewController : NSResponder

Overview

View controller management includes:

  • Memory management of top-level objects similar to that performed by the NSWindowController class, taking the same care to prevent reference cycles when controls are bound to the nib file's owner.

  • Declaring a generic 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

  2. viewWillAppear

  3. viewDidAppear

    User interaction cycle:

    1. updateViewConstraints

    2. viewWillLayout

    3. viewDidLayout

  4. viewWillDisappear

  5. 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 and override its loadView method to call [super loadView]. But in macOS 10.10 and later, the 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 initWithNibName:bundle: method.

A view controller employs lazy loading of its view: Immediately after a view controller is loaded into memory, the value of its viewLoaded property is NO. The value changes to YES after the loadView method returns and just before the system calls the viewDidLoad method.

A view controller is meant to be highly reusable, such as for dynamically representing various objects. For example, the addAccessoryController: methods of the NSPageLayout and NSPrintPanel classes take an NSViewController instance as the argument, and set the view property to the 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 class's key-value coding and key-value observing compliance. When the user dismisses a printing dialog, the NSPageLayout and 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

- initWithNibName:bundle:

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

- loadView

Instantiates a view from a nib file and sets the value of the view property.

Represented Object

representedObject

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

Nib Properties

nibBundle

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

nibName

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

View Properties

view

The view controller’s primary view.

title

The localized title of the receiver’s primary view.

NSEditor Conformance

- commitEditingWithDelegate:didCommitSelector:contextInfo:

Attempt to commit any currently edited results of the receiver.

- commitEditing

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

- discardEditing

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

Using a Storyboard

storyboard

The storyboard from which the view controller was loaded.

Responding to View Events

- viewDidLoad

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

viewLoaded

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

- 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

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

- viewWillDisappear

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

- viewDidDisappear

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

Managing View Layout

preferredContentSize

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

- updateViewConstraints

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

- viewWillLayout

Called just before the layout method of the view controller's view is called.

- viewDidLayout

Called immediately after the layout method of the view controller's view is called.

Managing Child View Controllers in a Custom Container

- addChildViewController:

A convenience method for adding a child view controller at the end of the childViewControllers array.

childViewControllers

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

- transitionFromViewController:toViewController:options:completionHandler:

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

- insertChildViewController:atIndex:

Inserts a specified child view controller into the childViewControllers array at a specified position.

- removeChildViewControllerAtIndex:

Removes a specified child controller from the view controller.

- removeFromParentViewController

Removes the called view controller from its parent view controller.

- preferredContentSizeDidChangeForViewController:

Called when there is a change in value of the preferredContentSize property of a child view controller or a presented view controller.

Presenting Another View Controller’s Content

- presentViewController:animator:

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

- dismissViewController:

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

- presentViewControllerAsModalWindow:

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

- presentViewControllerAsSheet:

Presents another view controller as a sheet.

Getting Related View Controllers

parentViewController

The immediate ancestor view controller of the view controller.

presentedViewControllers

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

presentingViewController

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

Configuring an App Extension View Controller

extensionContext

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

preferredScreenOrigin

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

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

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.

- viewWillTransitionToSize:

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

Constants

NSViewControllerTransitionOptions

Animation options for view transitions in a view controller.

Initializers