Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Counting down with timer

erdinc27erdinc27 TurkeyPosts: 393Registered Users @ @
i want to countdown from a string. So first i convert string to date
NSString *theDate = @17:22:19;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@en_US]];
[formatter setDateFormat:@HH:mm:ss];
NSDate *generatedDate = [formatter dateFromString:theDate];
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:generatedDate repeats:YES];
[timer fire];

and then in timer method
- (void)handleTimer:(NSTimer*)theTimer {
    
    NSDate *paramDate = (NSDate *)[theTimer userInfo];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @HH:mm:ss;
    
    NSDate *correctDate = [paramDate dateByAddingTimeInterval:-1.0];
    
    NSLog (@Got the string: %@", [formatter stringFromDate:correctDate]);
    
}

but it doesnt countdown. It just displays the same result always. So how i can make it countdown properly ?

Replies

  • RodoxydRodoxyd FrancePosts: 12New Users *
    Hi, try this:
    @property (strong, nonatomic) NSDate* countdownDate;
    
    NSString *theDate = @17:22:19;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@en_US]];
    [formatter setDateFormat:@HH:mm:ss];
    self.countdownDate = [formatter dateFromString:theDate];
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:formatter repeats:YES];
    [timer fire];
    
    - (void)handleTimer:(NSTimer*)theTimer {
        NSDateFormatter *formatter = (NSDateFormatter*)[theTimer userInfo];
        self.countdownDate = [self.countdownDate dateByAddingTimeInterval:-1.0];
        NSLog (@Got the string: %@", [formatter stringFromDate:self.countdownDate]);
    }
    
  • erdinc27erdinc27 TurkeyPosts: 393Registered Users @ @
    Rodoxyd wrote: »
    Hi, try this:
    @property (strong, nonatomic) NSDate* countdownDate;
    
    NSString *theDate = @17:22:19;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@en_US]];
    [formatter setDateFormat:@HH:mm:ss];
    self.countdownDate = [formatter dateFromString:theDate];
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:formatter repeats:YES];
    [timer fire];
    
    - (void)handleTimer:(NSTimer*)theTimer {
        NSDateFormatter *formatter = (NSDateFormatter*)[theTimer userInfo];
        self.countdownDate = [self.countdownDate dateByAddingTimeInterval:-1.0];
        NSLog (@Got the string: %@", [formatter stringFromDate:self.countdownDate]);
    }
    

    Thank you man. It is working now
Sign In or Register to comment.