Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [80]

By Root 880 0
greenColor] set];

[NSBezierPath fillRect: bounds];

// Draw the path in white

[[NSColor whiteColor] set];

[path stroke];

}

@end

Build and run your app. Pretty, eh?

OK, now try replacing [path stroke] with [path fill]. Build and run it.

NSScrollView


In the art world, a larger work is typically more expensive than a smaller one of equal quality. Your beautiful view is lovely, but it would be more valuable if it were larger. How can it be larger, yet still fit inside that tiny window? You are going to put it in a scroll view (Figure 17.12).

Figure 17.12. Completed Application

A scroll view has three parts: the document view, the content view, and the scroll bars. In this example, your view will become the document and will be displayed in the content view, which is an instance of NSClipView.

It looks tricky, but this change is very simple to make. In fact, it requires no code at all. Open MainMenu.xib in Interface Builder. Select the view, and choose Embed -> Scroll View from the Editor menu (Figure 17.13).

Figure 17.13. Embed StretchView in a Scroll View

As the window resizes, you want the scroll view to resize, but you do not want your document to resize. Open the Size Inspector, select the scroll view, and set the Size Inspector so that it resizes with the window (Figure 17.14).

Figure 17.14. Make Scroll View Resize with Window

Note the width and height of the view.

To select the document view, double-click inside the scroll view. You should see the title in the rightmost part of the jump bar change to Stretch View. Make the view about twice as wide and twice as tall as the scroll view. Set the Size Inspector so that the view will stick to the lower-left corner of its superview and not resize (Figure 17.15). Build the application and run it.

Figure 17.15. Make StretchView Larger and Nonresizing

Creating Views Programmatically


You will instantiate most of your views in the Interface Builder editor. Every once in a while, you will need to create views programmatically. For example, assume that you have a pointer to a window and want to put a button on it. This code would create a button and put it on the window’s content view:

NSView *superview = [window contentView];

NSRect frame = NSMakeRect(10, 10, 200, 100);

NSButton *button = [[NSButton alloc] initWithFrame:frame];

[button setTitle:@"Click me!"];

[superview addSubview:button];

For the More Curious: Cells


NSControl inherits from NSView. NSView (with its graphics context) is a relatively large and expensive object to create. When the NSButton class was created, the first thing someone did was to create a calculator with ten rows and ten columns of buttons. The performance was less than it could have been, because of the 100 tiny views. Later, someone had the clever idea of moving the brains of the button into another object (not a view) and creating one big view (called an NSMatrix) that would act as the view for all 100 button brains. The class for the button brains was called NSButtonCell (Figure 17.16).

Figure 17.16. NSMatrix

In the end, NSButton became just a view that had an NSButtonCell. The button cell does everything, and NSButton simply claims a space in the window (Figure 17.17).

Figure 17.17. NSButton and NSButtonCell

Similarly, NSSlider is a view with an NSSliderCell, and NSTextField is a view with an NSTextFieldCell. By contrast, NSColorWell has no cell.

To create an instance of NSMatrix in the Interface Builder editor, drop a control with a cell onto the window, choose Editor -> Embed In -> Matrix, and then Option-drag the size handles until the matrix has the correct number of rows and columns (Figure 17.18).

Figure 17.18. A Matrix of Buttons

An NSMatrix has a target and an action. A cell may also have a target and an action. If the cell is activated, the cell’s target and action are used. If the target and action of the selected cell are not set, the matrix’s target and action will be used.

When dealing with matrices, you will often ask which

Return Main Page Previous Page Next Page

®Online Book Reader