NSTimer仅在模拟器上不使用StopWatch App(显示静态奇怪数字)

时间:2012-03-29 13:08:24

标签: iphone xcode ios-simulator nstimer stopwatch

当我点击开始时,stopWatchLabel显示以下内容(其静态,未运行):

  • 注意:当我在iPhone上测试此应用时,所有按预期运行。没问题。

有人可以解释原因吗?

enter image description here

·H

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSTimer *stopWatchTimer; 
    NSDate *startDate;
}


@property (strong, nonatomic) IBOutlet UILabel *stopWatchLabel;

- (IBAction)startButtonTapped:(id)sender;

- (IBAction)stopButtonTapped:(id)sender;

-(void)updateTimer;

@end

的.m

- (IBAction)startButtonTapped:(id)sender {

    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                                      target:self 
                                                    selector:@selector(updateTimer) 
                                                    userInfo:nil 
                                                     repeats:YES];
}


- (void)updateTimer
{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    stopWatchLabel.text = timeString;
}


- (IBAction)stopButtonTapped:(id)sender {

    [stopWatchTimer invalidate];
}

1 个答案:

答案 0 :(得分:0)

您没有设置startDate

你的代码应该是这样的(我假设这是ARC):

- (IBAction)startButtonTapped:(id)sender {
    startDate = [NSDate date];

    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                                      target:self 
                                                    selector:@selector(updateTimer) 
                                                    userInfo:nil 
                                                     repeats:YES];
}

和updateTimer是一个荒谬的复杂方法,可以将秒分为几分钟和几秒。做一些基本的数学。

- (void)updateTimer
{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];

    NSInteger minutes = floor(timeInterval/60);
    NSInteger seconds = trunc(timeInterval - minutes * 60);

    NSString *timeString=[NSString stringWithFormat:@"%i:%02i", minutes, seconds];
    stopWatchLabel.text = timeString;
}

编辑:实际上你应该使用内置的NSCalendar / NSDateComponents API,因为它不会像基本的数学方法那样忽略夏令时。

- (void)updateTimer
{
    NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSSecondCalendarUnit|NSMinuteCalendarUnit fromDate:startDate toDate:currentDate options:0];
    NSInteger minutes = [components minute];
    NSInteger seconds = [components second];

    NSString *timeString=[NSString stringWithFormat:@"%i:%02i", minutes, seconds];
    stopWatchLabel.text = timeString;
 }