Home / iPad

Preparing for User Settings

Before diving into the code that animates the view in DeepThoughts, keep in mind one of the important tenets of object-oriented programming. The idea that you should keep the details of how an object works hidden from the other objects that use it.

In the case of DeepThoughts, the actual text that is shown in animation as well as the actual speed of the animation are details that should not be hardcoded into your view controller. In next tutorial, I show you how to enable the user to change the text and the speed setting, so you want to keep these details generic in the code you add to the view controller.

You also need to add a method to the view controller that connects to the Light Info button added to the user interface. This button will enable the user to change the text and speed settings. For now, this method will be a placeholder until you fill it out with code in next tutorial.

Editing the view controller header

With DeepThoughts, the idea is to enable the user to enter his or her own words for the falling words animation, as well as change the speed of the animation. The Light Info button, is supposed to react to a Touch Up Inside event - which occurs when the button is touched and then released, symbolizing a click. When that happens, it invokes a method in the view controller to display a modal view, which is a view presented modally like a dialog for the user to enter new words and change the speed setting.

For now, you need to declare an action method by using the IBAction qualifier found in DeepThoughtsViewController.h and add a placeholder method corresponding to it in DeepThoughtsViewController.m. You need to use the IBAction type qualifier, which is used by Interface Builder to synchronize actions added programmatically with its internal list of action methods defined for a project. You also need to add the code needed to display the falling words at a certain speed.

First open DeepThoughtsViewController.h (the header file) and insert the code in bold in Listing 1-1.

Listing 1-1: DeepThoughtsViewController.h
@interface DeepThoughtsViewController : UIViewController
    UIImage *fallingImage;
    NSString *fallingWords;
    UIImageView *imageView;
    double speed;
- (IBAction)settings;

@property (readwrite) double speed;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) NSString *fallingWords;

This code establishes the falling image itself (fallingImage), which will contain the text string in fallingWords and will flow down the display according to the speed.

Following that code is the action method declaration using the IBAction qualifier. You use Interface Builder to specify the fact that, when the user taps the Light Info button, the target is the DeepThoughts ViewController object and the method to invoke is settings.

The @property declarations declare that there are accessor methods for the compiler to create, and the corresponding @synthesize statements you add in the next section tell the compiler to actually create them for you.

You're done with the DeepThoughtsViewController.h file for this tutorial, but before you edit the code in DeepThoughtsViewController.m, you need to add the keys for your settings in a Constants.h file.

[Previous] [Contents] [Next]