Online Book Reader

Home Category

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

By Root 869 0
the views will be selected as the user tabs from one element to the next. The order will be the text field on the left, the text field on the right, the BigLetterView, and then back to the text field on the left.

Set the left-hand text field’s nextKeyView to be the right-hand text field (Figure 19.6).

Figure 19.6. Set nextKeyView of Left-Hand Text Field

Set the right-hand text field’s nextKeyView to be the BigLetterView (Figure 19.7).

Figure 19.7. Set nextKeyView of Right-Hand Text Field

Finally, set the nextKeyView of the BigLetterView to be the left-hand text field (Figure 19.8). This will enable the user to tab between the three views. Shift-tabbing will move the selection in the opposite direction.

Figure 19.8. Set nextKeyView of the BigLetterView

Which view, then, should be the firstResponder when the window first appears? To make the BigLetterView the initialFirstResponder of the window, Control-click on the title bar of the window in the editor to see the Connection panel, then drag from the initialFirstResponder outlet to the BigLetterView (Figure 19.9). You can also use the icon of the window on the dock to set the outlet.

Figure 19.9. Set the initialFirstResponder of the Window

Write the Code


In this section, you will make your BigLetterView respond to key events and accept first-responder status. The characters typed by the user will appear in the console. The completed application will look like Figure 19.10.

Figure 19.10. Completed Application

In BigLetterView.h

Your BigLetterView will have two instance variables and accessor methods for those variables. The bgColor variable will identify the background color of the view and will be an NSColor object. The string variable will hold onto the letter that the user most recently typed and will be an NSString object.

#import

@interface BigLetterView : NSView {

NSColor *bgColor;

NSString *string;

}

@property (strong) NSColor *bgColor;

@property (copy) NSString *string;

@end

In BigLetterView.m

The designated initializer for a view is initWithFrame:. In this method, you will call the superclass’s initWithFrame: method and initialize bgColor and string to default values. Add the following methods to BigLetterView.m:

- (id)initWithFrame:(NSRect)frame

{

self = [super initWithFrame:frame];

if (self)

{

NSLog(@"initializing view");

bgColor = [NSColor yellowColor];

string = @" ";

}

return self;

}

Create accessor methods for bgColor and string:

#pragma mark Accessors

- (void)setBgColor:(NSColor *)c

{

bgColor = c;

[self setNeedsDisplay:YES];

}

- (NSColor *)bgColor

{

return bgColor;

}

- (void)setString:(NSString *)c

{

string = c;

NSLog(@"The string is now %@", string);

}

- (NSString *)string

{

return string;

}

Add the following code to the drawRect: method to fill the view with bgColor. If it is the window’s firstResponder, the view will stroke a blue rectangle around its bounds to show the user that it will be the view receiving keyboard events:

- (void)drawRect:(NSRect)rect

{

NSRect bounds = [self bounds];

[bgColor set];

[NSBezierPath fillRect:bounds];

// Am I the window's first responder?

if ([[self window] firstResponder] == self) {

[[NSColor keyboardFocusIndicatorColor] set];

[NSBezierPath setDefaultLineWidth:4.0];

[NSBezierPath strokeRect:bounds];

}

}

The system can optimize your drawing a bit if it knows that the view is completely opaque. Override NSView’s isOpaque method:

- (BOOL)isOpaque

{

return YES;

}

The methods to become firstResponder are as follows:

- (BOOL)acceptsFirstResponder

{

NSLog(@"Accepting");

return YES;

}

- (BOOL)resignFirstResponder

{

NSLog(@"Resigning");

[self setNeedsDisplay:YES];

return YES;

}

- (BOOL)becomeFirstResponder

{

NSLog(@"Becoming");

[self setNeedsDisplay:YES];

return YES;

}

Once the view becomes the first responder, it will handle key events. For most keyDowns, the view will simply change string to be whatever the user typed. If, however, the

Return Main Page Previous Page Next Page

®Online Book Reader