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 01-03-2009, 02:26 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
kieran12 is on a distinguished road
Default NSTimer in label

Hey guys. I can't seem to work this one out at all. I've set up an NSTimer like this:

Code:
	NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
I have a label called timeLeft. I want it to count to down from 30 seconds to zero using the NSTimer. I can't figure out how to get the value of the NSTimer however. I have a variable int countdown set up to store the value but I can;t figure out how to retrieve it.

Any help appreciated.
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 01-03-2009, 02:48 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
kieran12 is on a distinguished road
Default

I have tried this still with no success:

Code:
NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];	
	countdown = [time fireDate];
	
	
}

-(void) onTimer{
	
	timeLeft.text = [NSString stringWithFormat:@"%i", countdown];
	
}

Any ideas?
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 01-03-2009, 02:48 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
exorcyze is on a distinguished road
Default

The timer itself doesn't keep any values ( like time elapsed ) - you'll have to create a variable yourself and update it inside the event when the timer fires.
exorcyze is offline   Reply With Quote
Old 01-03-2009, 02:49 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
kieran12 is on a distinguished road
Default

How do i do that? Is it similar to the code i just posted above?
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 01-03-2009, 03:38 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
kieran12 is on a distinguished road
Default

Ok here is the code I have got so far. Everything builds fine and works except the label with the countdown won;t update. Please help.

Code:
-(void)timer{
	
	time = 30.0;
	timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];	
	
}
-(IBAction)startGame:(id)sender{
	
	
	
	[mainView timer];
	
	
}



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

-(void)countDown {
	time = time - 1;
	if (time == 0.0) {
		[timer invalidate];
	}
	[mainView updateLabel];
}
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 01-03-2009, 03:48 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
kieran12 is on a distinguished road
Default

Ok finally figured it out. There was a mistake in the last bit of code I posted. timerWithInterval should have been scheduledTimerWithInterval. It is now working
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 01-03-2009, 03:50 PM   #7 (permalink)
Tutorial Author
 
Steaps's Avatar
 
Join Date: Oct 2008
Location: Ontario, Canada
Posts: 464
Steaps is on a distinguished road
Default

I have used this tutorial before, it worked well, and it is exactly what you are looking for.
Welcome to the iPhone Development Central Website!
Just skip the animation part, but the countdown timer is there.

EDIT: Oh, you got it working.
Steaps is offline   Reply With Quote
Old 07-24-2009, 01:49 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 141
eski is on a distinguished road
Default

i did this by following the tutorial but even though iv put in

time = time - 1;

it counts down in 4s! i have no idea why and have tried changing the numer but it just times 4 by what numer i put in

e.g time = time - 4; makes it count down in 16s

any one know what could be the problem??
eski is offline   Reply With Quote
Old 08-10-2009, 10:38 AM   #9 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 2
macack is on a distinguished road
Default

Quote:
Originally Posted by eski View Post
i did this by following the tutorial but even though iv put in

time = time - 1;

it counts down in 4s! i have no idea why and have tried changing the numer but it just times 4 by what numer i put in

e.g time = time - 4; makes it count down in 16s

any one know what could be the problem??
Bump... I am doing a countup routine (vs. the above countdown) and my label seems to be incrementing by 4 every seconds. Any solutiosn for this?

Thx.
macack is offline   Reply With Quote
Old 08-10-2009, 01:27 PM   #10 (permalink)
Registered Member
 
rdominelli's Avatar
 
Join Date: Jul 2009
Posts: 107
rdominelli is on a distinguished road
Default

Quote:
Originally Posted by macack View Post
Bump... I am doing a countup routine (vs. the above countdown) and my label seems to be incrementing by 4 every seconds. Any solutiosn for this?

Thx.
Don't use a counter. Instead use now + 30 seconds and figure out the difference.

Code:
float period = 30;

self.myAlarmDate = [[NSDate alloc] initWithTimeIntervalSinceNow:(NSTimeInterval)period];
Then on your timer tick
Code:
NSTimeInterval tmptimeleft = [self.myAlarmDate timeIntervalSinceNow];

label.text = [NSString StringWithFormat:@"%f",tmptimeleft];
NSTimeInterval is actually a double so format it as you want.

Rich
rdominelli is offline   Reply With Quote
Reply

Bookmarks

Tags
countdown, nstimer

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: 407
17 members and 390 guests
7twenty7, blasterbr, buggen, chiataytuday, Clouds, dre, fiftysixty, HemiMG, jeroenkeij, jimmyon122, jonathandeknudt, LEARN2MAKE, n00b, nyoe, pungs, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,901
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

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