아래 코드를 실행하려고하지만 "Tick"이 콘솔에 기록 된 후 시뮬레이터가 계속 잠겨 있습니다. 내 생각에 "Tock"을 출력하지 않으므로 "NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];"와 관련이 있습니다. IBactions는 버튼으로 활성화됩니다. timer 및 startTime은 .h에서 NSTimer 및 NSDate로 각각 정의됩니다.NSTimer Troubles
누구든지 내가 뭘 잘못하고 있다고 말할 수 있습니까?
코드 :
- (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
내 .H 코드로 게시물을 업데이트 : – SonnyBurnette
'해야한다', 나는 @properties에 너무 익숙하지 않다. 그러나 내가 undertand 할 수 있었던 것에서, 그것은해야한다. 그러나 내 테스트에서는 그렇지 않습니다. 나는 그것이 "startTime = [NSDate date]; 일을하기 때문이라고 생각합니다." 합성되고있는 setter 속성을 거치지 않고 객체에 직접 액세스하고 있습니다 (또한 @synthesize, right?를 호출하고 있습니다). 내 방법은 작동합니다 .-D – ACBurk
개체에 액세스하지 않고 개체를 인스턴스 변수에 직접 할당합니다. 그게 문제가있는 곳입니다. 너는 그 물건을 통과하지 않을거야. –