我有一个 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
提前致谢!