以下のコードを実行しようとしていますが、コンソールに「Tick」が書き込まれた後もシミュレーターがロックされ続けます。「Tock」は出力されないので、「NSTimeIntervallapsedTime =[startTimetimeIntervalSinceNow];」という行に関係していると思います。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