01-03-2009, 02:26 PM
#1 (permalink )
Registered Member
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
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.
01-03-2009, 02:48 PM
#2 (permalink )
Registered Member
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
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?
01-03-2009, 02:48 PM
#3 (permalink )
Registered Member
Join Date: Dec 2008
Posts: 495
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.
01-03-2009, 02:49 PM
#4 (permalink )
Registered Member
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
How do i do that? Is it similar to the code i just posted above?
01-03-2009, 03:38 PM
#5 (permalink )
Registered Member
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
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];
}
01-03-2009, 03:48 PM
#6 (permalink )
Registered Member
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
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
01-03-2009, 03:50 PM
#7 (permalink )
Tutorial Author
Join Date: Oct 2008
Location: Ontario, Canada
Posts: 464
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.
07-24-2009, 01:49 PM
#8 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 141
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??
08-10-2009, 10:38 AM
#9 (permalink )
New Member
Join Date: Aug 2009
Posts: 2
Quote:
Originally Posted by
eski
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.
08-10-2009, 01:27 PM
#10 (permalink )
Registered Member
Join Date: Jul 2009
Posts: 107
Quote:
Originally Posted by
macack
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
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» 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