Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

Next Page > Hide TOC

NSView Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/AppKit.framework
Availability
Available in Mac OS X v10.0 and later.
Declared in
NSClipView.h
NSMenuItem.h
NSView.h
Companion guides

Class at a Glance

NSView is an abstract class that defines the basic drawing, event-handling, and printing architecture of an application. You typically don’t interact with the NSView API directly; rather, your custom view classes inherit from NSView and override many of its methods, which are invoked automatically by the Application Kit. If you’re not creating a custom view class, there are few methods you need to use.

Principal Attributes

Commonly Used Methods

frame

Returns the location and size of the NSView object.

bounds

Returns the internal origin and size of the NSView object.

setNeedsDisplay:

Marks the NSView object as needing to be redrawn

window

Returns the NSWindow object that contains the NSView object.

drawRect:

Draws the NSView object. (All subclasses must implement this method, but it’s rarely invoked explicitly.)

Overview

NSView is an abstract class that provides concrete subclasses with a structure for drawing, printing, and handling events.

NSView objects (also know, simply, as view objects or views) are arranged within an NSWindow object, in a nested hierarchy of subviews. A view object claims a rectangular region of its enclosing superview, is responsible for all drawing within that region, and is eligible to receive mouse events occurring in it as well. In addition to these major responsibilities, NSView handles dragging of icons and works with the NSScrollView class to support efficient scrolling.

Most of the functionality of NSView either is automatically invoked by the Application Kit, or is available in Interface Builder. Unless you’re implementing a concrete subclass of NSView or working intimately with the content of the view hierarchy at runtime, you don’t need to know much about this class’s interface. See “Commonly Used Methods” for methods you might use regardless.

For more information on how NSView instances handle event and action messages, see Handling Events in Views, Handling Mouse Events in Views, and Handling Tracking-Rectangle and Cursor-Update Events in Views. For more information on displaying tooltips and contextual menus, see How Contextual Menus Work and Tooltips.

Subclassing Notes

NSView is perhaps the most important class in the Application Kit when it comes to subclassing and inheritance. Most user-interface objects you see in a Cocoa application are objects that inherit from NSView. If you want to create an object that draws itself in a special way, or that responds to mouse clicks in a special way, you would create a custom subclass of NSView (or of a class that inherits from NSView). Subclassing NSView is such a common and important procedure that several technical documents describe how to both draw in custom subclasses and respond to events in custom subclasses. See Cocoa Drawing Guide and Cocoa Event-Handling Guide (especially "Handling Events in Views" and "Handling Mouse Events in Views").

Tasks

Creating Instances

Managing the View Hierarchy

Searching by Tag

Modifying the Frame Rectangle

Modifying the Bounds Rectangle

Modifying the Coordinate System

Examining Coordinate System Modifications

Base Coordinate Conversion

Converting Coordinates

Controlling Notifications

Resizing Subviews

Focusing

Displaying

Focus Ring Drawing

Fullscreen Mode

Hiding Views

Drawing

Managing Live Resize

Managing the Graphics State

Event Handling

Dragging Operations

Tool Tips

Managing Tracking Rectangles

Managing Tracking Areas

Managing Cursor Tracking

Scrolling

Contextual Menus

Key-view Loop Management

Printing

Pagination