0

我有一个标签 00:00.0 和按钮。当我按下按钮时,无论我点击多少次,计时器都应该在标签上计数。当我点击标签时计时器应该停止,并且应该在第二次点击同一标签时重置。

我尝试了一切,但我无法达到结果:) 任何人请帮助我。

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
    IBOutlet UILabel *stopWatchLabel;
    NSTimer *stopWatchTimer; // Store the timer that fires after a certain time
    NSDate *startDate; // Stores the date of the click on the start button
}

@property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel;
    - (IBAction)onStartPressed:(id)sender;
    - (IBAction)onStopPressed:(id)sender;
    - (void)updateTimer;
4

2 回答 2

1

在我的活动项目中引用一些代码。

    -(void)start{
        [self setup];
        if(_timer == nil){
            _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireInterval target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];
            _counting = YES;
        }
    }


    -(void)pause{
        [_timer invalidate];
        _timer = nil;
        _counting = NO;
    }

这就是实现秒表的基本概念,如果你想使用 UILabel 作为秒表,MZTimerLabel是一个完美的两行解决方案。看一看。

干杯。

于 2013-10-16T14:20:12.147 回答
1
-(IBAction)onStartpressed:(id)sender{
     NSTimer * timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1.0 target:self selector:@selector(methodthatUpdateslabel) userInfo:nil repeats:YES];
     [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

}    

-(IBAction)onStopPressed:(id)sender{
[timer invalidate];
}

这应该可以,希望对您有所帮助。

于 2012-06-20T12:22:36.980 回答