Now that we’ve got the main view up and running, let’s build the flipside view. As you can see in Figure 10-19, the flipside view features our warp drive switch, as well as the warp factor slider. We’ll use the same controls that the Settings application uses for these two items: a switch and a slider. First, we need to declare our outlets, so single-click FlipsideViewController.h, and make the following changes:
@protocol FlipsideViewControllerDelegate;
@interface FlipsideViewController : UIViewController { id <FlipsideViewControllerDelegate> delegate;
UISwitch *engineSwitch;
UISlider *warpFactorSlider;
}
@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UISwitch *engineSwitch;
@property (nonatomic, retain) IBOutlet UISlider *warpFactorSlider;
- (IBAction)done;
@end
@protocol FlipsideViewControllerDelegate - (void)flipsideViewControllerDidFinish:
(FlipsideViewController *)controller;
@end
NoTe
Don’t worry too much about the extra code here.
As we saw before, the Utility Application template makes MainViewController a delegate of the
FlipsideViewController, the extra code here that hasn’t been in the other file templates we’ve used implements that delegate relationship.
Now, double-click FlipsideView.xib to open it in Inter- face Builder. If the Flipside View window is not open, double-click the Flipside View icon in the nib’s main window to open it. First, change the background color using the attribute inspector to a lighter shade of gray, about a 25% gray should work well. The default flipside view background color is too dark for black text to look good, but light enough that white text is hard to read.
Next, drag two Labels from the library and place them on Flipside View window. Double-click one of them and change it to read Warp Engines:. Double-click the other, and call it Warp Factor:. You can use Figure 10-19 as a placement guide.
Figure 10-19. Desiging the flipside view in Interface Builder
Download at Boykma.Com
When you’re done placing the controls, double-click the word Title at the top of the view and change it to read Warp Settings.
Next, drag over a Switch from the library, and place it against the right side of the view across from the label that reads Warp Engines. Control-drag from the File’s Owner icon to the new switch, and connect it to the engineSwitch outlet.
Now drag over a Slider from the library, and place it below the label that reads Warp Factor. Resize the slider so that it stretches from the blue guide line on the left margin to the one on the right, and then control-drag from the File’s Owner icon to the slider, and connect it to the warpFactorSlider outlet.
Single-click the slider if it’s not still selected, and press
⌘1 to bring up the attributes inspector. Set Minimum to 1.00, Maximum to 10.00, and Initial to 5.00. Next, select turtle.png for Min Image and rabbit.png for Max Image.
Once you’re done, the inspector should look like Figure 10-20.
Save and close the nib, and head back to Xcode so we can finish the flipside view controller. Single-click FlipsideViewController.m, and make the following changes:
#import "FlipsideViewController.h"
#import "MainViewController.h"
@implementation FlipsideViewController
@synthesize delegate;
@synthesize engineSwitch;
@synthesize warpFactorSlider;
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
engineSwitch.on = ([[defaults objectForKey:kWarpDriveKey]
isEqualToString:@"Engaged"]) ? YES : NO;
warpFactorSlider.value = [defaults floatForKey:kWarpFactorKey];
[super viewDidLoad];
}
Figure 10-20. The attributes inspec- tor for our Warp Factor slider
- (void)viewWillDisappear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *prefValue = (engineSwitch.on) ? @"Engaged" : @"Disabled";
[defaults setObject:prefValue forKey:kWarpDriveKey];
[defaults setFloat:warpFactorSlider.value forKey:kWarpFactorKey];
[super viewWillDisappear:animated];
} ...
Add the following lines of code to the existing dealloc and viewDidUnload methods:
...
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.engineSwitch = nil;
self.warpFactorSlider = nil;
[super viewDidUnload];
}
- (void)dealloc {
[engineSwitch release];
[warpFactorSlider release];
[super dealloc];
} ...
In the viewDidLoad method, we deleted one line of code and added three (well, four, because one line was too long to fit the page width of this book). The one line of code we deleted wasn’t really important. Code in the template set the background color of the view using a class method, and that line of code caused the flipside view to have a textured, dark gray appearance rather than using the background that was set in Interface Builder. The textured background made it difficult to read the text and to see the slider pictures that we used; we deleted it to let the background color from Interface Builder shine through so our text and icons could be seen more easily.
The four lines of code we added get a reference to the standard user defaults and use the outlets for the switch and slider to set them to the values stored in the user defaults. Because we opted to store strings rather than Booleans for the warp drive setting, we have to handle the conversion in our code because a UISwitch instance is set using a BOOL property.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
engineSwitch.on = ([[defaults objectForKey:kWarpDriveKey]
isEqualToString:@"Engaged"]) ? YES : NO;
warpFactorSlider.value = [defaults floatForKey:kWarpFactorKey];
Download at Boykma.Com
We also overrode our parent’s viewWillDisappear: method so that we could stuff the values from our controls back into the user defaults before the main view is shown again.
Because our controller’s viewDidDisappear: method will fire before the main view’s
viewWillAppear: method, the changed values will already be stored in the user defaults for the view to retrieve, so the main view will get updated with the correct new values.