Instance Method


Informs the view that it’s being added to the view hierarchy of the specified window object (which may be nil).


func viewWillMove(toWindow newWindow: NSWindow?)



The window object that will be at the root of the view's new view hierarchy. If the view is being removed from a window and there is no new window, this parameter is nil.


AppKit calls this method when the window of a view changes. It also calls it in cases where a view stays in the same window but its position in its view hierarchy changes. The view that moved also calls this method on all of its subviews, giving each of them a chance to respond to the change.

Subclasses can override this method to perform whatever actions are necessary. For example, when a window is deallocated, you can use this method to remove notification observers and bindings associated with the view.

When a window is deallocated, AppKit calls this method for each view in the window, passing nil for the newWindow parameter. AppKit does not necessarily call this method when closing a window, though. Closing a window usually just hides the window. Closed windows are deallocated only if their isReleasedWhenClosed method returns true.

See Also

Managing the View Hierarchy

var superview: NSView?

The view that is the parent of the current view.

var subviews: [NSView]

The array of views embedded in the current view.

var window: NSWindow?

The view’s window object, if it is installed in a window.

func addSubview(NSView)

Adds a view to the view’s subviews so it’s displayed above its siblings.

func addSubview(NSView, positioned: NSWindow.OrderingMode, relativeTo: NSView?)

Inserts a view among the view’s subviews so it’s displayed immediately above or below another view.

func didAddSubview(NSView)

Overridden by subclasses to perform additional actions when subviews are added to the view.

func removeFromSuperview()

Unlinks the view from its superview and its window, removes it from the responder chain, and invalidates its cursor rectangles.

func removeFromSuperviewWithoutNeedingDisplay()

Unlinks the view from its superview and its window and removes it from the responder chain, but does not invalidate its cursor rectangles to cause redrawing.

func replaceSubview(NSView, with: NSView)

Replaces one of the view’s subviews with another view.

func isDescendant(of: NSView) -> Bool

Returns true if the view is a subview of a given view or if it’s identical to that view; otherwise, it returns false.

var opaqueAncestor: NSView?

The view’s closest opaque ancestor, which might be the view itself.

func ancestorShared(with: NSView) -> NSView?

Returns the closest ancestor shared by the view and another specified view.

func viewDidMoveToSuperview()

Informs the view that its superview has changed (possibly to nil).

func viewDidMoveToWindow()

Informs the view that it has been added to a new view hierarchy.

func viewWillMove(toSuperview: NSView?)

Informs the view that its superview is about to change to the specified superview (which may be nil).

func willRemoveSubview(NSView)

Overridden by subclasses to perform additional actions before subviews are removed from the view.

var enclosingMenuItem: NSMenuItem?

The menu item containing the view or any of its superviews in the view hierarchy.