开发者

How to set NSTimer in iphone sdk?

开发者 https://www.devze.com 2023-02-25 12:13 出处:网络
I\'m displaying 10 questions in a view controller. I want to calculate the time taken t开发者_Python百科o answer those questions.Means I want to set time count down after the time reaches to zero I wa

I'm displaying 10 questions in a view controller. I want to calculate the time taken t开发者_Python百科o answer those questions.Means I want to set time count down after the time reaches to zero I want to display an alert. Please help me.

Thanks in advance.


You seems to want 2 things.

One is elapsed time between the start of the question until it get answer, you can do that with:

NSDate *now = [NSDate date];
NSTimeInterval *elapsedTime = [now timeIntervalSinceNow];

Reference: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

The second is to use NSTimer for counting down. You can do that with the following code:

NSTimer *theTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdownTracker:) userInfo:nil repeats:YES];
// Assume a there's a property timer that will retain the created timer for future reference.
self.timer = theTimer;
// Assume there's a property counter that track the number of seconds before count down reach.
self.counter = 10;

The method that will be invoked by timer.

- (void)countdownTracker:(NSTimer *)theTimer {
    self.counter--;
    if (self.counter < 0) {
        [self.timer invalidate];
        self.timer = nil;
        self.counter = 0;
        [[[[UIAlertView alloc] initWithTitle:@"Countdown" message:@"Countdown completed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
    }
}

This should work.

Reference: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html


- (void) showClock
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"HH"];
    int hour = [[dateFormatter stringFromDate:[NSDate date]] intValue];
    [dateFormatter setDateFormat:@"mm"];
    int minute = [[dateFormatter stringFromDate:[NSDate date]] intValue];
    [dateFormatter release];
    NSTimeInterval remainingSec = [databaseDate timeIntervalSinceNow];
    if ( remainingSec >= 0)
    {
        timer = nil;

        self.databaseDate = [NSDate dateWithTimeIntervalSinceNow:0];
        remainingSec = [databaseDate timeIntervalSinceNow];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self 
                        selector:@selector(showClock) 
                        userInfo:nil 
                        repeats:YES];
    }

    NSInteger hours = -remainingSec / 3600;
    NSInteger remainder = ((NSInteger)remainingSec)% 3600;
    NSInteger minutes = -remainder / 60;
    NSInteger seconds = -remainder % 60;

    //NSString * stringvalue=[[NSString alloc]initWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    int hoursvalue=hour+hours;
    int minutsvalue=minute+minutes;
    stopWatchLabel.text=[NSString stringWithFormat:@"%i:%i",hoursvalue,minutsvalue]; 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号