0

Iam using xCode 4.3.2, In my count-down timer project (code showing below), i want to stop the count-down after 30 seconds. How can i set this limit for my count-down? Could you please help me?

int countLimit=30; //seconds
NSDate *startDate;

- (void)countDown{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = -([currentDate timeIntervalSinceDate:startDate]);
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    self.myCounterLabel.text = timeString;
    [dateFormatter release];
}

- (void)updateCounter{

    self.myCounterLabel.text = @"00:00:00";
    startDate = [[NSDate date]retain];

    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
    target:self
    selector:@selector(countDown)
    userInfo:nil
    repeats:YES];
}
shebi
  • 707
  • 2
  • 14
  • 23
  • I couldn't get this code work, but [here's a great tutorial](http://iphoneapp-dev.blogspot.com/2010/10/how-to-create-countdown-timer-in-iphone.html) on how to make a countdown timer which works perfect for me. – Neeku Dec 04 '12 at 15:59

1 Answers1

0

Just add to your countDown

if([[NSDate date] timeIntervalSinceDate:startDate]>=countLimit)

[myTimer invalidate];
Nikita Pestrov
  • 5,876
  • 4
  • 31
  • 66
  • it gives me two warnings:- 1. Class method'+timeIntervalSinceDate:' not found(return type defaults to 'id'). 2. Ordered comparison between pointer and integer('id' and 'int') – shebi Apr 14 '12 at 10:06