Online Book Reader

Home Category

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

By Root 831 0
job of the destination to determine what happens.

The dragging info object will do most of the work for you. It will get the source’s advertised operation mask and filter it, depending on what modifier keys the user holds down. To see this, implement draggingUpdated: and log out the dragging info’s operation mask:

- (NSDragOperation)draggingUpdated:(id )sender

{

NSDragOperation op = [sender draggingSourceOperationMask];

NSLog(@"operation mask = %ld", op);

if ([sender draggingSource] == self) {

return NSDragOperationNone;

}

return NSDragOperationCopy;

}

Now build and run the application. Try dragging text from different sources and holding down different modifier keys. Note what happens to the mask and the cursor.

Chapter 24. NSTimer


An instance of NSButton has a target and a selector (the action). When the button is clicked, the action message is sent to the target. Timers work in a similar way. A timer is an object that has a target, a selector, and a delay, which is given in seconds (Figure 24.1). After the delay, the selector message is sent to the target. The timer sends itself as an argument to the message. The timer can also be set to send the message repeatedly.

Figure 24.1. NSTimer

To play with timers a bit, you will create a typing tutor application. The application will have two BigLetterView objects. One will display what the user should type, and the other will display what the user has typed (Figure 24.2). An NSProgressIndicator will display how much time is left. After 2 seconds, the application will beep to indicate that the user took too long. Then the user is given 2 more seconds.

Figure 24.2. Completed Application

You will create a TutorController class. When the user clicks the Go button, an instance of NSTimer will be created. The timer will send a message every 0.1 second. The method triggered will check whether the two views match. If so, the user is given a new letter to type. Otherwise, the progress indicator is updated to reflect the elapsed time. If the user clicks the Stop button, the timer is invalidated. Figure 24.3 shows the object diagram.

Figure 24.3. Object Diagram

Go back to your TypingTutor project in Xcode. Create a new Objective-C class called TutorController. In TutorController.h, give it two outlets and an action. You will also need a timer, an array for the letters, the index of the last letter displayed, and three time intervals that will help track how long the current letter has been visible:

#import

@class BigLetterView;

@interface TutorController : NSObject {

// Outlets

IBOutlet BigLetterView *inLetterView;

IBOutlet BigLetterView *outLetterView;

// Data

NSArray *letters;

int lastIndex;

// Time

NSTimeInterval startTime;

NSTimeInterval elapsedTime;

NSTimeInterval timeLimit;

NSTimer *timer;

}

- (IBAction)stopGo:(id)sender;

- (void)updateElapsedTime;

- (void)resetElapsedTime;

- (void)showAnotherLetter;

@end

Lay Out the Interface


Open MainMenu.xib. Create an instance of TutorController by dragging an Object into the editor (from under Cocoa -> Objects & Controllers). Set the class to be TutorController (Figure 24.4).

Figure 24.4. Create an Instance of TutorController

Add two labels (from Cocoa -> Controls) and place them above each of the BigLetterView instances. Set the left label to read Type Here and the right label to Match This.

Drop an NSProgressIndicator onto the window. Use the Inspector to make it not indeterminate. Set its range to be 0 to 100 (Figure 24.5).

Figure 24.5. Inspect the Progress Indicator

Put a Rounded Textured Button onto the window. Using the Attributes Inspector, set its title to Go and its alternate title to Stop. Set its type to Toggle and its state to Off (Figure 24.6).

Figure 24.6. Inspect the Button

Make Connections


Control-drag from the button to the TutorController object. Set the action to be stopGo: (Figure 24.7).

Figure 24.7. Connect the Button to the TutorController

Open the Bindings Inspector

Return Main Page Previous Page Next Page

®Online Book Reader