iPad Application Development For Dummies by Neal Goldstein

iPad Application Development For Dummies by Neal Goldstein

Author:Neal Goldstein
Language: eng
Format: epub
Publisher: John Wiley & Sons, Ltd.
Published: 2010-04-07T16:00:00+00:00


Xcode creates SettingsViewController.h and SettingsViewController.m in the Classes group. Xcode also creates SettingsView.xib in the Classes group. You may want to drag SettingsView.xib from that group into the Resources-iPad group, as I do in Figure 11-3, just to be consistent. (That’s where the other nib files are located.)

Figure 11-2: Here you name the subclass.

Figure 11-3: Move the nib (.xib) file to the Resources-iPad group.

You now have a bare-bones view controller for the Modal view, called SettingsViewController.

Next, you need to add the code to the SettingsViewController.h (header) and SettingsViewController.m (implementation) files that connect to the interface elements (speed slider and text entry field) to offer the ability to change the speed and enter replacement text.

Adding outlets to the view controller

Before using Interface Builder to create the elements for the Modal view, you should first put outlets in the code that will connect your methods to the Interface Builder interface objects.

The fact that a connection between an object and its outlets exists is actually stored in a nib file. When the nib file is loaded, each connection is reconstituted and reestablished, thus enabling you to send messages to the object. IBOutlet is the keyword that tags an instance-variable declaration so the Interface Builder application knows that a particular instance variable is an outlet — and can then enable the connection to it with Xcode.

In your code, it turns out that you need to create two outlets: one to point to the text entry field and one to point to the speed slider. To get this outlet business started, you need to declare each outlet, which you do with the help of the aforementioned IBOutlet keyword.

Add the bold lines of code in Listing 11-1 to the SettingsViewController.h file.

Listing 11-1: SettingsViewController.h

#import <UIKit/UIKit.h>

@protocol SettingsViewControllerDelegate;

@interface SettingsViewController : UIViewController {

<SettingsViewControllerDelegate> delegate;

NSString *wordsOfWisdom;

float sliderValue;

IBOutlet UITextField *theTextField;

IBOutlet UISlider *slider;

}

- (IBAction) done;

- (IBAction) speedChanged: (id) sender;

@property (nonatomic, assign) id <SettingsViewControllerDelegate> delegate;

@property (nonatomic, assign) NSString* wordsOfWisdom;

@property (nonatomic, assign) UISlider* slider;

@end

@protocol SettingsViewControllerDelegate

- (void) settingsViewControllerDidFinish:(SettingsViewController *)controller;

- (void) changeSpeed: (double) newSpeed;

@end

Two action methods (done and speedChanged) for Interface Builder elements are declared (with IBAction), along with the IBOutlet statements, which declare the outlets that will be automatically initialized with a pointer to the UITextField (theTextField) and the UISlider (slider) when the application is launched. But while this will happen automatically, it won’t automatically happen automatically. You have to help it out a bit.

In procedural programming — you know, all that Linux Kernel stuff — variables are generally fair game for all. But in object-oriented programming, a class’s instance variables are tucked away inside an object and shouldn’t be accessed directly. The only way for them to be initialized is for you to create what are called accessor methods, which allow the specific instance variable of an object to be read and (if you want) updated. Creating accessor methods is a two-step process that begins with a @property declaration, which tells the compiler that there are accessor methods. And that’s what you did in Listing 11-1; you coded corresponding @property declarations for each IBOutlet declaration.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.