Online Book Reader

Home Category

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

By Root 873 0
methods of NSSlider that you will use frequently:

- (void)setFloatValue:(float)x

Moves the slider to x.

- (float)floatValue

Returns the current value of the slider.

NSTextField


An instance of NSTextField can allow a user to type a single line of text. Text fields may or may not be editable. Uneditable text fields are commonly used as labels on a window. Compared to buttons and sliders, text fields are relatively complex. We will plumb the depths of the mysteries surrounding text fields in later chapters. Figure 5.6 shows the Attributes information panel for an NSTextField in Interface Builder.

Figure 5.6. Text Field Inspector

Text fields have a placeholder string. When the text field is empty, the placeholder string is displayed in gray.

NSSecureTextField is a subclass of NSTextField and is used for such things as passwords. As the user types, bullets appear instead of the typed characters. You cannot copy or cut from an NSSecureTextField.

Here are a few of the most commonly used NSTextField methods:

- (NSString *)stringValue

- (void)setStringValue:(NSString *)aString

Allow you to get and set the string data being displayed in the text field.

- (NSObject *)objectValue

- (void)setObjectValue:(NSObject *)obj

Allow you to get and set the data being displayed in the text field as an arbitrary object type. This behavior is helpful if you are using a formatter. NSFormatters are responsible for converting a string into another type, and vice versa. If no formatter is present, these methods use the description method.

For example, you might use a text field to allow the user to type in a date. As the programmer, you don’t want the string that the user typed in; you want an instance of NSDate. By attaching an NSDateFormatter, you ensure that the text field’s objectValue method will return an NSDate. Also, when you call setObjectValue: with an NSDate, the NSDateFormatter will format it as a string for the user. (You will create a custom formatter class in Chapter 26.)

Figure 5.7 shows some other controls you might want to play with. Drag them out, inspect them, and see how they act when you compile and run the app.

Figure 5.7. Some Controls

Start the SpeakLine Example


As a simple example of using controls, you will build an application that enables users to type in a line of text and hear it spoken by the Mac OS X speech synthesizer. The app will look like Figure 5.8 when you are done with this chapter.

Figure 5.8. Completed Application

Figure 5.9 presents a diagram of the objects that you will create and their pointers to one another. Note that all the classes that start with NS are part of the Cocoa frameworks and thus already exist. Your code will be in the SpeakLineAppDelegate class, which Xcode creates as part of the project template.

Figure 5.9. Object Diagram

In Xcode, create a new project. Name the project SpeakLine and set the Class Prefix to SpeakLine. A new project will appear.

Lay Out the XIB File


Click on MainMenu.xib to open it in Interface Builder. Click the window icon in Interface Builder’s dock to open it. With the window selected, uncheck Resize in the Attributes Inspector. We will still be able to resize the window in the editor, but the user will not.

Next, drag out a text field and two buttons from the Library panel. Double-click on the text field to change the text to read “Peter Piper picked a peck of pickled peppers” (or some other text that will amuse you when it is spoken by the machine). Change the labels on the buttons to read Speak and Stop. The result should look like Figure 5.8.

The SpeakLineAppDelegate class, which Xcode created as part of our project, will be the target of the two buttons. Each control will trigger a different action method.

With MainMenu.xib still selected, enable the Assistant Editor, using the toolbar button (Figure 5.10). The Assistant Editor shows a counterpart to the currently selected file. The counterpart for MainMenu.xib is the application delegate header file, SpeakLineAppDelegate.h.

Return Main Page Previous Page Next Page

®Online Book Reader