我创建了一个简单地计时分钟的 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)];
}
-(void)tick{
timeTick++;
NSString *timeString = [[NSString alloc] initWithFormat:@"%d", timeTick];
labelTime.text = timeString;
}
@end
提前致谢!