Online Book Reader

Home Category

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

By Root 820 0
the sheet window and the window to which it is attached, you supply a modal delegate, a selector, and a contextInfo pointer when you start the sheet. The modalDelegate will be sent the didEndSelector, and the sheet, its return code, and the contextInfo will be sent as arguments. Thus, the method triggered by the didEndSelector should have a signature like this:

- (void)rex:(NSWindow *)sheet

fido:(NSInteger)returnCode

rover:(void *)contextInfo;

The dog names are used here to indicate that you could name the method anything you wish. Most programmers name the method something more meaningful, such as sheetDidEnd:returnCode:contextInfo:.

Adding a Sheet


You are going to add a sheet that will allow the user to adjust the speed of TypingTutor. You will bring up the sheet when the user selects the Adjust speed... menu item. You will end the sheet when the user clicks the OK button. The final application will look like Figure 25.1.

Figure 25.1. Completed Application

Your TutorController will control the slider and the window, so you will need to add outlets for them. Also, your TutorController will be sent a message when the user selects the Adjust speed... menu item or clicks the OK button, so you will need to add two action methods to the TutorController.

Figure 25.2 presents the object diagram.

Figure 25.2. Object Diagram

Add Outlets and Actions


Edit TutorController.h as follows:

#import

@class BigLetterView;

@interface TutorController : NSObject

{

// Outlets

IBOutlet BigLetterView *inLetterView;

IBOutlet BigLetterView *outLetterView;

IBOutlet NSWindow *speedSheet;

// Data

NSArray *letters;

int lastIndex;

// Time

NSTimeInterval startTime;

NSTimeInterval timeLimit;

NSTimeInterval elapsedTime;

NSTimer *timer;

}

- (IBAction)stopGo:(id)sender;

- (IBAction)showSpeedSheet:(id)sender;

- (IBAction)endSpeedSheet:(id)sender;

- (void)updateElapsedTime;

- (void)resetElapsedTime;

- (void)showAnotherLetter;

@end

Save the file.

Lay Out the Interface


Open MainMenu.xib. Add a menu item to the main menu for your application by dragging it out of the Library (under Cocoa -> Windows & Menus) (Figure 25.3).

Figure 25.3. Add a Menu Item

Change the title of the menu item to Adjust Speed.... Control-drag from the menu item to the TutorController. Set the action to be showSpeedSheet: (Figure 25.4).

Figure 25.4. Connect the Menu Item

Create a new window by dragging one out of the Library (under Cocoa -> Windows & Menus) (Figure 25.5). Disable resizing for the window. Uncheck Visible at launch.

Figure 25.5. Inspect New Window

Put a slider on the new window. To label the left end of the slider as “1 second” and the right end as “10 seconds,” drop two labels onto the window. Add a third label above the slider to read Time per letter:. Add a button and change its title to OK. Inspect the slider and set its range to be 1 to 10 (Figure 25.6).

Figure 25.6. Inspect Slider

Bind the Value of the slider to the TutorController’s timeLimit as shown in Figure 25.7.

Figure 25.7. Bind the Slider’s Value to timeLimit

When the user clicks the OK button, it should send to the TutorController a message that will end the sheet. Control-drag from the button to the TutorController and choose endSpeedSheet: as the action (Figure 25.8).

Figure 25.8. Set the Target of the Button

To raise the window as a sheet, your TutorController must have a pointer to it. Control-click on the TutorController to get the Connection panel. Connect the speedSheet outlet to the window (Figure 25.9).

Figure 25.9. Connect speedSheet Outlet

Add Code


When the user chooses the Adjust Speed... menu item, the sheet will run. Add the following method to TutorController.m:

- (IBAction)showSpeedSheet:(id)sender

{

[NSApp beginSheet:speedSheet

modalForWindow:[inLetterView window]

modalDelegate:nil

didEndSelector:NULL

contextInfo:NULL];

}

Note that you are attaching the sheet to the window that the inLetterView is on. Also, when

Return Main Page Previous Page Next Page

®Online Book Reader