CS185c
Chris Pollett
Oct. 5, 2009
#import "SwitchViewController.h"
#import "BlueViewController.h"
#import "YellowViewController.h"
@implementation SwitchViewController
@synthesize blueViewController;
@synthesize yellowViewController;
- (void)viewDidLoad {
// set up blue controller
BlueViewController *blueController = [[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil];
self.blueViewController = blueController;
// initial use blue view
[self.view insertSubview:blueController.view atIndex: 0];
//index 0 behind everything
[blueController release];
}
- (void) switchViews:(id)sender
{
// load Yellow nib first time button is pressed
if(self.yellowViewController == nil)
{
YellowViewController *yellowController =
[[YellowViewController alloc]
initWithNibName:@"YellowView" bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
//check if not Blue View
if(self.blueViewController.view.superview == nil)
{
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
}
else
{
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
}
}
//... rest of boilerplate code unchanged
- (void)dealloc {
[blueViewController release];
[yellowViewController release];
[super dealloc];
}
@end
Which of the following statements is true:
#import <UIKit/UIKit.h>
@interface BlueViewController : UIViewController {
}
-(IBAction) blueButtonPressed: (id) sender;
@end
#import "BlueViewController.h"
@implementation BlueViewController
-(IBAction) blueButtonPressed:(id) sender
{
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];
}
//... unchanged boiler plate
@end
- (void) switchViews:(id)sender
{
// load Yellow nib first time button is pressed
if(self.yellowViewController == nil)
{
YellowViewController *yellowController =
[[YellowViewController alloc]
initWithNibName:@"YellowView" bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
// next control speed at begin and end of animation
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
UIViewController *coming = nil;
UIViewController *going = nil;
UIViewAnimationTransition transition;
if(self.blueViewController.view.superview == nil)
{
coming = blueViewController;
going = yellowViewController;
transition = UIViewAnimationTransitionFlipFromLeft;
/*
Other possibilities:
UIViewAnimationTransitionFlipFromRight
UIViewAnimationTransitionCurlUp
UIViewAnimationTransitionCurlDown
*/
}
else
{
coming = yellowViewController;
going = blueViewController;
transition = UIViewAnimationTransitionFlipFromRight;
}
//cache makes an image of view in cache and
//then animate image rather than redraw view
[UIView setAnimationTransition:transition forView:self.view cache: YES];
[coming viewWillAppear:YES];
[going viewWillDisappear:YES];
[going.view removeFromSuperview];
[self.view insertSubview:coming.view atIndex:0];
[going viewDidDisappear:YES];
[coming viewDidAppear:YES];
[UIView commitAnimations];
}