The two content views that we are creating in this application are extremely simple. They each have one action method that is triggered by a button, and neither one needs any out- lets. The two views are also nearly identical. In fact, they are so similar that they could have been represented by the same class. We chose to make them two separate classes, because that’s how most multiview applications are constructed. Let’s declare an action method in each of the header files. First, in BlueViewController.h, add the following declaration:
#import <UIKit/UIKit.h>
@interface BlueViewController : UIViewController {
}
-(IBAction)blueButtonPressed;
@end
Save it, and then add the following line to YellowViewController.h:
#import <UIKit/UIKit.h>
@interface YellowViewController : UIViewController {
}
- (IBAction)yellowButtonPressed;
@end
Save this one as well, and then double-click BlueView.xib to open it in Interface Builder so we can make a few changes. First, we have to tell it that the class that will load this nib from disk is BlueViewController, so single-click the File’s Owner icon and press ⌘4 to bring up the identity inspector. File’s Owner defaults to NSObject; change it to BlueViewController.
Single-click the icon called View and then press ⌘1 to bring up the attribute inspector. Click the color well that’s labeled Background, and change the background color of this view to a nice shade of blue.
Next, we’ll change the size of the view in the nib. In the attribute inspector, the top section is labeled Simulated User Interface Elements. If we set these drop-downs to reflect which top and bottom elements are used in our application, Interface Builder will automatically calcu- late the size of the remaining space. The status bar is already specified. If you select the Bottom Bar pop-up, you can select Toolbar to indicate that the enclosing view has a toolbar. By setting this, Interface Builder will automatically calculate the correct size for our view so that you know how much space you have to work with. You can press ⌘3 to bring up the size inspector to confirm this. After making the change, the height of the window should now be 416, and the width should still be 320.
Drag a Round Rect Button from the library over to the window. Double-click the button, and change its title to Press Me. You can place the button anywhere that looks good to you. Next, switch to the connections inspector (by pressing ⌘2), drag from the Touch Up Inside event to the File’s Owner icon, and connect to the blueButton- Pressed action method.
We have one more thing to do in this nib, which is to connect the BlueViewController’s
view outlet to the view in the nib. The view outlet is inherited from the parent class,
UIViewController, and gives the controller access to the view it controls. When we changed the underlying class of the file’s owner, the existing outlet connections were broken. As a result, we need to reestablish the connection from the controller to its view.
Control-drag from the File’s Owner icon to the View icon, and select the view outlet to do that.
Save the nib, go back to Xcode, and double-click YellowView.xib. We’re going to make almost the same exact changes to this nib file. We need to change the file’s owner from NSObject to YellowViewController using the identity inspector, change the view’s height to 416 pixels using the size inspector, and change the view’s background to a nice yellow color using the attributes inspector. You’ll also need to add a round rectangular button to this view, give it a label of Press Me, Too, and connect that button’s Touch Up Inside event to the
Figure 6-17. The Simulated User Interface Elements section of the View’s attributes inspector
yellowButtonPressed action method in File’s Owner. Finally, control-drag from the File’s Owner icon to the View icon, and connect to the view outlet.
Once all that is done, save the nib, and go back to Xcode.
The two action methods we’re going to implement do nothing more than show an alert, something you already know how to do, so go ahead and add the following code to BlueViewController.m:
#import "BlueViewController.h"
@implementation BlueViewController
- (IBAction)blueButtonPressed {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Blue View Button Pressed"
message:@"You pressed the button on the blue view"
delegate:nil
cancelButtonTitle:@"Yep, I did."
otherButtonTitles:nil];
[alert show];
[alert release];
} ...
Save, switch over to YellowViewController.m, and add this very similar code to that file:
#import "YellowViewController.h"
@implementation YellowViewController -(IBAction)yellowButtonPressed {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Yellow View Button Pressed"
message:@"You pressed the button on the yellow view"
delegate:nil
cancelButtonTitle:@"Yep, I did."
otherButtonTitles:nil];
[alert show];
[alert release];
} ...
Save it, and we’re ready to try it. When our application launches, it’ll show the view we built
that we built in YellowView.xib. Tap it again, and it goes back to the view we built in BlueView.
xib. Whether you tap the button on the blue or yellow view, you’ll get an alert view with a message indicating which button was pressed. This alert shows us that the correct controller class is getting called for the view that is being shown.
The transition between the two views is kind of abrupt, though. Gosh, if only there were some way to make the transition look nicer.