0

我有一个 NSTimer,我需要一个重置按钮来重置屏幕上的变量并停止该计时器,直到它由开始按钮再次启动。这是一些代码:

@implementation TimeController

int timeTick = 0;


NSTimer *timer;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    labelTime.text = @"0";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)startTimer:(id)sender {
    [timer invalidate];
    timer= [NSTimer scheduledTimerWithTimeInterval:60.0 target:(self) selector:(@selector(tick)) userInfo:(nil) repeats:(YES)];
}

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

}

-(void)tick{
    timeTick++;
    NSString *timeString = [[NSString alloc] initWithFormat:@"%d", timeTick];
    labelTime.text = timeString;



}


@end

提前致谢!

4

2 回答 2

3

看起来您已经启动了一个停止计时器的功能。只需在那里重置变量。

- (IBAction)resetTicktock:(id)sender {
    [timer invalidate];
    labelTime.text = @"0";
}

将您的按钮链接到情节提要中的此方法,以执行“Touch Up Inside”操作。

于 2013-11-08T16:35:12.543 回答
0

您需要重置 timeTick 和标签的文本

- (IBAction)resetTicktock:(id)sender {
   [timer invalidate];
   labelTime.text = @"0";
   timeTick = 0;
}
于 2016-01-11T10:15:13.523 回答