Two Very Important Classes: NSWindow and NSView

In this section we’ll describe the very important NSWindow and NSView classes in more detail and list many of their most useful instance methods.

The NSWindow Class

NSWindow is one of the most important classes in the Application Kit. If you want to be an effective Cocoa programmer, it is essential that you be familiar with the variety and scope of NSWindow’s many methods.

Every on-screen window displayed by a program is controlled by an instance object of the NSWindow class. Each NSWindow object receives events from a program’s NSApplication object. Most mouse events are sent to the object within the window where the mouse event took place (e.g., if an on-screen button is clicked, a mouseDown: message is sent to the corresponding NSButton object). Keyboard events are sent to the object that is the window’s first responder, which we’ll describe in detail in the next chapter.

Each window contains at least one instance of the NSView class (described shortly), called the window’s content view . Although you can work directly with a window’s content view, normally you will create subviews of the content view in which you do your actual drawing and event processing. We’ll discuss these ideas in great depth in the following chapters.

Some of the most common instance methods for NSWindow objects are listed in Table 7-4. Recall that if an argument’s data type isn’t specified (e.g., sender), it’s an id by default.

Table 7-4. Common instance methods for NSWindow objects

NSWindow instance method

Purpose

- (id)contentView

Returns the id of the window’s content view.

- (void)makeKeyAndOrderFront:(id)sender

Makes the window the key window and places it in front of all other windows on the screen.

- (void)center

Moves the window to the center of the screen.

- (void)orderOut:(id)sender

Takes the window out of the screen list, which makes it invisible. The window is still in the Window Server’s memory; you just can’t see it!

- (void)performClose:(id)sender

Simulates a user’s clicking the window’s close button.

- (void)performMiniaturize:(id)sender

Simulates a user’s clicking the window’s miniaturize button.

- (void)setFrame:(NSRect)aFrame display:(BOOL)flag animate:(BOOL)aFlag

Moves and optionally resizes a window with or without smooth animation.

-(BOOL)setFrameUsingName:(NSString *)aName

Sets the window’s frame using a name stored in the system defaults. This is handy if you want a program to reappear in the same location on the screen as the location where it was last run.

- (void)setFrameFromString:(NSString *)aString

Sets the window’s frame using a representation stored in aString. This is handy if you want to remember a window’s position and size in a file and then restore it later.

- (void)setTitle:(NSString *)aString

Sets the window’s title in its title bar to aString.

- (void)setTitleWithRepresentedFilename: (NSString *)aString

Sets the window’s title in its title bar to a filename.

- (NSRect)frame

Returns the window’s frame; tells you where the window is on the screen and how big it is.

All of the methods available to the NSWindow class are described in the NSWindow documentation. One of the easiest ways to read this documentation is by referring to Apple’s Developer web site: http://developer.apple.com. You can also use the class browser that is built into PB. To do this, click the Classes vertical tab in PB’s main window, click the disclosure triangle next to NSObject, and then click the little book icon next to a class such as NSArray. (See Figure 7-16.)

Viewing NSArray documentation in PB

Figure 7-16. Viewing NSArray documentation in PB

The NSView Class

NSView is the basic class for creating objects that draw in windows and respond to user events. Just as everything drawn on a Cocoa screen is drawn in a window, practically everything drawn inside a window is drawn with the help of NSView objects. For example, the NSMatrix, NSTextField, and NSButton classes we’ve used in our Calculator application are all subclasses of NSView.

Every window contains at least one view — the content view. This view covers the window except for the title bar, resize handle, and border. The window’s content view automatically stretches and shrinks with the window when the window is resized.

Every view can have zero or more subviews. After a view draws itself, it redraws any of the objects in its subview hierarchy (we’ll say more about view hierarchies later) whose appearance has been changed or altered. In this way, what we see on the Mac OS X screen properly corresponds to what is stored in the computer’s memory.

The NSView class is one of the most powerful abstractions in Cocoa’s Application Kit. Some of its most useful methods are listed in Table 7-5.

Table 7-5. Common instance methods for NSView objects

NSView instance method

Purpose

- (void)addSubview:(NSView *)aView

Adds aView as a subview to the NSView.

- (void)display

Causes the NSView to redisplay itself and all of its subviews by invoking the drawRect: method for all of these views. Do not invoke this method; call setNeedsDisplay: instead.

- (void)drawRect:(NSRect)rect

Implemented by subclasses of the NSView class to draw themselves. This single method handles displaying on the screen, printing, and scrolling. You normally do not call this method, but instead allow it to be called by the NSView class.

- (void)lockFocus- (void)unlockFocus

Locks/unlocks the drawing focus on an NSView, so that all future Quartz drawing commands are executed in this NSView. If you are drawing inside a drawRect: method, focus is automatically locked and unlocked for your program (by the display method).

- (void)setNeedsDisplay:(BOOL)aFlag

Causes this view (and all of its subviews) to be redisplayed after the current event is finished being processed. Call this method with the argument YES rather than calling the display method.

- (NSArray *)subviews

Returns the NSArray object that contains all of an NSView’s subviews.

- (NSView *)superview

Returns the id of an NSView’s superview.

- (int)tag

Returns the NSView’s tag. By default, NSViews have a tag of -1, but some NSViews (such as NSControls) allow you to change their tags to distinguish them from one another.

- (id)viewWithTag:(int)aTag

Searches an NSView and all of its subviews for a view with a given tag.

- (NSWindow *)window

Returns the id of the NSView’s window.

The rest of the NSView methods are described at Apple’s Developer web site: http://developer.apple.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.96