Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-23-2009, 12:45 PM   #1 (permalink)
iOS Developer
 
chaseacton's Avatar
 
Join Date: Feb 2009
Location: United States
Posts: 541
chaseacton is on a distinguished road
Send a message via AIM to chaseacton Send a message via Skype™ to chaseacton
Default How to make button start timer?

Hello all, i am making a simple game where i need a button to activate a timer the counts down from 30 seconds, and then when it gets to 0 seconds, load another view. I have searched all day and found only bits and pieces of code that partially work, but nothing solid. Any code or sample projects would be appreciated.

Thanks, Chase

chaseacton@gmail.com
chaseacton is offline   Reply With Quote
Old 06-23-2009, 01:26 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

I dont think you've looked very hard then. I'll assume you know how to work with interface builder, and you know some very basic programming stuff.

Code:
-(IBAction)onButtonPress
{
  theTimer =    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector( updateTimerDisplay) userInfo:nil repeats:YES];
}

-(void)updateTimerDisplay
{
     //set label text or whatever to current time left
     //decrement current time left

    if( timeLeft <=0 )  // a class var you can setup yourself. Hope you can do this
     {
       [theTimer invalidate]; //this stops the timer
      

      //now you can load the next view
     // ...
     }
}
smithdale87 is offline   Reply With Quote
Old 06-23-2009, 02:09 PM   #3 (permalink)
iOS Developer
 
chaseacton's Avatar
 
Join Date: Feb 2009
Location: United States
Posts: 541
chaseacton is on a distinguished road
Send a message via AIM to chaseacton Send a message via Skype™ to chaseacton
Default

Quote:
Originally Posted by smithdale87 View Post
I dont think you've looked very hard then. I'll assume you know how to work with interface builder, and you know some very basic programming stuff.

Code:
-(IBAction)onButtonPress
{
  theTimer =    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector( updateTimerDisplay) userInfo:nil repeats:YES];
}

-(void)updateTimerDisplay
{
     //set label text or whatever to current time left
     //decrement current time left

    if( timeLeft <=0 )  // a class var you can setup yourself. Hope you can do this
     {
       [theTimer invalidate]; //this stops the timer
      

      //now you can load the next view
     // ...
     }
}
I actually just figured it out before you told me.

My only question now, is how do i load another view when the timer reaches 0?
chaseacton is offline   Reply With Quote
Old 06-23-2009, 02:51 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

It depends on where you're calling the timer from.

I usually keep different views in different xib files, so when I want to switch views, this is how I do it:

Code:
MyNewViewController vc = [[MyNewViewController alloc] initWithNibName:@"MyNewViewController" bundle:nil];
[self.navigationController pushViewController: vc animated:YES];
[vc release];
smithdale87 is offline   Reply With Quote
Old 06-23-2009, 03:09 PM   #5 (permalink)
iOS Developer
 
chaseacton's Avatar
 
Join Date: Feb 2009
Location: United States
Posts: 541
chaseacton is on a distinguished road
Send a message via AIM to chaseacton Send a message via Skype™ to chaseacton
Default

Quote:
Originally Posted by smithdale87 View Post
It depends on where you're calling the timer from.

I usually keep different views in different xib files, so when I want to switch views, this is how I do it:

Code:
MyNewViewController vc = [[MyNewViewController alloc] initWithNibName:@"MyNewViewController" bundle:nil];
[self.navigationController pushViewController: vc animated:YES];
[vc release];
Thanks for the quick reply, but that doesnt help. I need to open up another view within the same xib, when the timer reaces 0.


my code looks like this. what and where would i insert the code to do this?

I think it needs to be put around where "[timer invalidate];" is.

Code:
#import "MainView.h"

@implementation MainView
@synthesize timer, countLabel;
int count = 1;

-(void)awakeFromNib {
	
	counter.text = @"0";
	
}

- (IBAction)startTimer {
	time = 30.0;
	timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector:@selector (countDown) userInfo:nil repeats:YES];
}

-(void)countDown {
	time = time - 1;
	if (time == 0) {
		[timer invalidate];
			}
	[self updateLabel];
}


-(void)updateLabel {
	countLabel.text = [NSString stringWithFormat:@"%i" , time];
}

- (IBAction)addUnit {
	
	if(count >= 999) return;
	
	NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count++];
	counter.text = numValue;
	[numValue release];
}

- (IBAction)subtractUnit {
	
	if(count <= 0) return;
	
	NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count--];
	counter.text = numValue;
	[numValue release];	
}



@end

Last edited by chaseacton; 06-23-2009 at 03:13 PM.
chaseacton is offline   Reply With Quote
Old 06-23-2009, 03:16 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Connect your 2nd view to an IBOutlet.

Then, when timer reaches zero, just do self.view = secondView;
smithdale87 is offline   Reply With Quote
Old 06-23-2009, 04:49 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 60
wearmaize is on a distinguished road
Default

Can't you just make an "if" statement?

if label.text == 0 {
// load another view //
}
wearmaize is offline   Reply With Quote
Reply

Bookmarks

Tags
clock, countdown, nstimer, timer

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 333
8 members and 325 guests
chiataytuday, givensur, ipodphone, jbro, mtl_tech_guy, Punkjumper, vilisei, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,881
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:14 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0