CS185c
Chris Pollett
Oct. 4, 2010
@interface NSTimerDemoViewController : UIViewController {
NSTimer *timer;
}
-(void) delayedResponse:(NSTimer*)theTimer;
-(IBAction) buttonPressed: (id) sender;
@end
-(IBAction) buttonPressed:(id) sender
{
timer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target:self
selector:@selector(delayedResponse:) userInfo:nil repeats:NO];
}
-(void) delayedResponse:(NSTimer*)theTimer {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Button Pressed -- Took A while"
message:@"You pressed the button, but I was napping"
delegate:nil
cancelButtonTitle:@"Yep, I did"
otherButtonTitles:nil];
[alert show];
[alert release];
}
After writing the above code, we connect the button up to buttonPressed in interface builder and the program should work.
Which of the following is true? (More than one possible)
this.OrientationChanged +=
new EventHandler(OnOrientationChanged);
@class SwitchViewController; @interface multiviewdemoAppDelegate : NSObject{ IBOutlet UIWindow *window; IBOutlet SwitchViewController *switchViewController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet SwitchViewController *switchViewController; @end
#import "multiviewdemoAppDelegate.h" #import "SwitchViewController.h" @implementation multiviewdemoAppDelegate @synthesize window; @synthesize switchViewController; - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after application launch [window addSubview:switchViewController.view]; [window makeKeyAndVisible]; } - (void)dealloc { [window release]; [switchViewController release]; [super dealloc]; } @end
#import <UIKit/UIKit.h>
@class BlueViewController; //similar to declaring something external in C
@class YellowViewController;
@interface SwitchViewController : UIViewController {
YellowViewController *yellowViewController;
BlueViewController *blueViewController;
}
@property (retain, nonatomic) YellowViewController *yellowViewController;
@property (retain, nonatomic) BlueViewController *blueViewController;
-(IBAction) switchViews:(id) sender;
@end
#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
#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];
}