我正在尝试运行下面的代码,但在将“Tick”写入控制台后,它会一直锁定我的模拟器。它从不输出“Tock”,所以我的猜测是它与“NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];”行有关 IBactions 由按钮激活。timer 和 startTime 在 .h 中分别定义为 NSTimer 和 NSDate。
谁能告诉我我做错了什么?
代码:
- (IBAction)startStopwatch:(id)sender
{
startTime = [NSDate date];
NSLog(@"%@", startTime);
timer = [NSTimer scheduledTimerWithTimeInterval:1 //0.02
target:self
selector:@selector(tick:)
userInfo:nil
repeats:YES];
}
- (IBAction)stopStopwatch:(id)sender
{
[timer invalidate];
timer = nil;
}
- (void)tick:(NSTimer *)theTimer
{
NSLog(@"Tick!");
NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];
NSLog(@"Tock!");
NSLog(@"Delta: %d", elapsedTime);
}
我在.h中有以下内容:
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
NSTimer *timer;
NSDate *startTime;
}
- (IBAction)startStopwatch:(id)sender;
- (IBAction)stopStopwatch:(id)sender;
- (void)tick:(NSTimer *)theTimer;
@property(nonatomic, retain) NSTimer *timer;
@property(nonatomic, retain) NSDate *startTime;
@end