Next: Creating a Button Up: Adding a Button in Previous: Adding a Button in

A Quick Introduction to Views

NSView is a class whose objects represent a rectangular area in a window. Each NSView has its own internal coordinate system; NSView provides a complete framework for managing the rectangle represented by the view, its coordinate system, and for drawing and getting user events inside the view's rectangle.

Any object which can be displayed (and/or can accept user events) inside a window (such as a button, a text field, a scroll view, a slider, etc) is actually an instance of a subclass of NSView. The specific subclass (for example, NSButton) implements a specific way of drawing and managing user events inside the view's rectangle. An unsubclassed NSView by default draws nothing and reacts to no user events, even if it contains all the machinery (ready for subclasses to use) to do both.

When you create a window, the library automatically creates an NSView covering the whole content area of the window. This view is called the window's content view. You can replace the default content view (which is transparent and reacts to no user events) with an NSView of your choice (which can be an instance of a subclass of NSView, such as a NSButton or a NSScrollView), but you should not change the rectangle it represents, which must always cover the whole window.

If you want to have a view which covers only a part of a window, you need to add it as a subview of the content view. We will explore subviews in a forth-coming tutorial; in our first example, we will go for the simplest possible solution avoiding use of subviews: we will create a single NSButton, make a window to fit exactly our button, and then replace the default window's content view with our button. In this way we'll get a window containing exactly our button. In the next tutorial, we'll learn how to enclose views one over the other in a subview tree, so that we can put more things in a single window.


Next: Creating a Button Up: Adding a Button in Previous: Adding a Button in
Nicola Pero 2000-07-29