iPhone Timers, Multiview Applications




CS185c

Chris Pollett

Oct. 4, 2010

Outline

Introduction

Timing on the iPhone

A view with a button on it

The Interface

@interface NSTimerDemoViewController : UIViewController {
	NSTimer *timer;
}
-(void) delayedResponse:(NSTimer*)theTimer;
-(IBAction) buttonPressed: (id) sender;
@end

New code in the Controller

-(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.

Quiz

Which of the following is true? (More than one possible)

  1. The following code would be legal in C#:
    this.OrientationChanged +=
        new EventHandler(OnOrientationChanged);
    
  2. SQLiteOpenHelper in Android might be used in the event that your app is upgraded from one version to the next.
  3. iPhone apps have a Documents directory and an app can read to and write from there.

Multiview Applications

More on Multiview Applications

Creating Our View Controller and Nib Files

Modifying the App Delegate Header

Modifying the App Delegate Implementation

SwitchViewController.h

SwitchViewController Implementation

#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

Implementing Content Views

BlueViewController Implementation

Animating the Transition