Reputation:
i want to count down time from 10 to 0. and em doing this by this code.. but its not working fine
-(void)elapsedTime
{
static int i = 11;
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(850, 27, 50, 50)];
label.textColor = [UIColor redColor];
label.font = [UIFont boldSystemFontOfSize:25.0f];
label.backgroundColor=[UIColor blackColor];
label.text = [NSString stringWithFormat:@"%i",i];
[self.view addSubview:label];
i--;
if(i<0)
{
[aTimer invalidate];
aTimer = nil;
}
[label sendSubviewToBack:self.view];
}
Upvotes: 1
Views: 1556
Reputation: 8387
If you creating some counter application like http://download.cnet.com/Work-Time-Counter/3000-2124_4-75903089.html
you need to use NSDate like follow:
-(void) start {
// Save start time
self.started = [[NSDate alloc] init];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateCallback)
userInfo:nil
repeats:YES];
}
-(void)updateCallback {
// Count time since start and notify UI
[self.callback counterTick:[ self.started timeIntervalSinceNow ]];
}
Upvotes: 0
Reputation: 801
in .h
UILabel *label;
in .m
- (void)viewDidLoad
{
label = [[UILabel alloc] initWithFrame:CGRectMake(850, 27, 50, 50)];
label.textColor = [UIColor redColor];
label.font = [UIFont boldSystemFontOfSize:25.0f];
label.backgroundColor=[UIColor blackColor];
label.text = @"10";
[self.view addSubview:label];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(elapsedTime:) userInfo:nil repeats:YES];
}
- (void)elapsedTime:(NSTimer *)timer
{
int currentValue = [label.text intValue];
currentValue--;
label.text = [NSString stringWithFormat:@"%i",currentValue];
if (currentValue == 0)
{
[self.view sendSubviewToBack:label];
[timer invalidate];
timer = nil;
}
}
Upvotes: 0
Reputation: 3510
try this
.h
NSTimer* time;
int count;
.m file
viedidload
count=10;
time=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeShedule) userInfo:nil repeats:YES];
-(void)timeShedule {
label.text=[NSString stringWithFormat:@"%d",count];
count--;
if (count==0) {
[time invalidate];
time=nil;
}
}
Upvotes: 0
Reputation: 9241
You should use NSTimer
for this puropose like this.
In .h file put
{
NSTimer *timer
}
In .m file put this
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
Now add a function, it will be called every 1 second. You can update your label in this function.
-(void)updateLabel {
if (timeCount==0) {
[timer invalidate];
} else {
timeCount = timeCount-1;
label.text = [NSString stringWithFormat:@"%d",timeCount];
}
}
Hope this helps.
Upvotes: 1
Reputation: 2132
Use below code for your app. and call addlabel from viewDidload and define label and i in .h file.
-(void)addlabel{
i=10;
label = [[UILabel alloc] initWithFrame:CGRectMake(150, 27, 50, 50)];
label.textColor = [UIColor redColor];
label.font = [UIFont boldSystemFontOfSize:25.0f];
label.backgroundColor=[UIColor blackColor];
label.text = [NSString stringWithFormat:@"%i",i];
[self.view addSubview:label];
aTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(elapsedTime) userInfo:nil repeats:YES];
}
-(void)elapsedTime{
i--;
label.text = [NSString stringWithFormat:@"%i",i];
if(i<1)
{
[aTimer invalidate];
aTimer = nil;
}
}
Upvotes: 0
Reputation: 2741
You can do this by using NSTimer.
Just follow the links it will give u detailed knowledge about timers.
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Timers/Timers.html
http://pravinmagdum.wordpress.com/2010/12/30/how-to-use-nstimer/
http://samplecodebank.blogspot.in/2011/05/nstimer-example.html
Upvotes: 0