Online Book Reader

Home Category

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

By Root 841 0
of the Progress Indicator (Figure 24.8). Bind Value to TutorController’s model key elapsedTime. Bind Max Value, also on the Progress Indicator, to the tutor controller’s timeLimit.

Figure 24.8. NSProgressIndicator Bindings

Control-click on the TutorController to display the Connection panel. Drag from inLetterView to the BigLetterView on the left. Drag from outLetterView to the BigLetterView on the right (Figure 24.9).

Figure 24.9. Connecting the outLetterView Outlet

Add Code to TutorController


Implement the following methods in TutorController.m:

#import "TutorController.h"

#import "BigLetterView.h"

@implementation TutorController

- (id)init

{

self = [super init];

if (self) {

// Create an array of letters

letters = [NSArray arrayWithObjects:@"a", @"s",

@"d",@"f", @"j", @"k", @"l", @";", nil];

// Seed the random number generator

srandom((unsigned)time(NULL));

timeLimit = 5.0;

}

return self;

}

- (void)awakeFromNib

{

[self showAnotherLetter];

}

- (void)resetElapsedTime

{

startTime = [NSDate timeIntervalSinceReferenceDate];

[self updateElapsedTime];

}

- (void)updateElapsedTime

{

[self willChangeValueForKey:@"elapsedTime"];

elapsedTime = [NSDate timeIntervalSinceReferenceDate] - startTime;

[self didChangeValueForKey:@"elapsedTime"];

}

- (void)showAnotherLetter

{

//Choose random numbers until you get a different

// number than last time

int x = lastIndex;

while (x == lastIndex){

x = (int)(random() % [letters count]);

}

lastIndex = x;

[outLetterView setString:[letters objectAtIndex:x]];

// Start the count again

[self resetElapsedTime];

}

- (IBAction)stopGo:(id)sender

{

[self resetElapsedTime];

if (timer == nil) {

NSLog(@"Starting");

// Create a timer

timer = [NSTimer scheduledTimerWithTimeInterval:0.1

target:self

selector:@selector(checkThem:)

userInfo:nil

repeats:YES];

} else {

NSLog(@"Stopping");

// Invalidate the timer

[timer invalidate];

timer = nil;

}

}

- (void)checkThem:(NSTimer *)aTimer

{

if ([[inLetterView string] isEqual:[outLetterView string]]) {

[self showAnotherLetter];

}

[self updateElapsedTime];

if (elapsedTime >= timeLimit) {

NSBeep();

[self resetElapsedTime];

}

}

@end

Build and run your application.

Note, once again, that we have separated our classes into views (BigLetterView) and controllers (TutorController). If we were creating a full-featured application, we would probably also create model classes, such as Lesson and Student.

For the More Curious: NSRunLoop


NSRunLoop is an object that waits. It waits for events to arrive and then forwards them to NSApplication. It waits for timer events to arrive and then forwards them to NSTimer. You can even attach a network socket to the run loop, and it will wait for data to arrive on that socket.

Challenge


Change your DrawingFun application so that autoscrolling is timer driven. Delete your mouseDragged: method from StretchView. In mouseDown:, create a repeating timer that invokes a method in the view every tenth of a second. In the invoked method, autoscroll using the current event. To get the current event, use NSApplication’s currentEvent method:

NSEvent *e = [NSApp currentEvent];

(Remember that NSApp is a global variable that points to the instance of NSApplication.) Invalidate the timer in mouseUp:. Note that the autoscrolling becomes much smoother and more predictable.

Chapter 25. Sheets


A sheet is simply an NSWindow instance that is attached to another window. The sheet comes down over the window, and the window stops getting events until the sheet is dismissed. Typically, you will compose a sheet as an off-screen window in your XIB file.

NSApplication has several methods that make sheets possible:

// Start a sheet

- (void)beginSheet:(NSWindow *)sheet

modalForWindow:(NSWindow *)docWindow

modalDelegate:(id)modalDelegate

didEndSelector:(SEL)didEndSelector

contextInfo:(void *)contextInfo;

// End the sheet

- (void)endSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode;

Besides

Return Main Page Previous Page Next Page

®Online Book Reader