2009-11-14 2 views
0

안녕하세요 저는 객관적으로 새로운 c입니다. 나는 아이폰 용 앱을 만들려고 노력 중이다. 내보기에 버튼이 있고 functionSound가 호출 된 클릭이 있습니다. 이것은 제대로 작동하고 있습니다. 그것은 내가 원하는 소리를 재생합니다. 이제 타이머에 문제가 있습니다. 같은 버튼을 클릭하면 타이머가 시작되고 타이머 값이 레이블에 표시됩니다. 아직 NSTimer 자체로는 분명하지 않습니다. 나는 내가 여기서 뭔가 잘못하고있는 것 같아. 누구든지 이걸 도와 줄 수 있어요.Iphone NSTimer Issue

-(IBAction)playSound { //:(int)reps 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"chicken" ofType:@"wav"]; 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
    theAudio.delegate = self; 
    [theAudio play]; 

    [self startTimer]; 
} 

- (void)startTimer { 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(targetMethod) userInfo:nil repeats:YES]; 
    labelA.text = [NSString stringWithFormat:@"%d", timer]; 
} 

위의 코드를 사용하면 버튼을 클릭하면 사운드가 재생되고 응용 프로그램이 닫힙니다.

감사 Zeeshan

답변

2

이 줄 :

labelA.text = [NSString stringWithFormat:@"%d", timer]; 

가 전혀 이해되지 않는다. 타이머가 발생하면 scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:의 선택기로 지정한 메서드를 호출하므로 해당 메서드를 구현하고 거기에서 레이블을 업데이트해야합니다. startTimer의 첫 번째 줄은 거의 정확하지만 (이것은 하나 개의 매개 변수를 사용하는 방법 의미 때문에) 선택기는 콜론을 포함해야합니다 : 나는 우리가 그 방법을 구현해야하므로 선택 timerFired: 이름

- (void)startTimer { 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; 
} 

참고. 당신은 타이머 카운터를 증가하려면, 당신도이 방법으로 그렇게해야합니다 :

- (void)timerFired:(NSTimer *)timer { 
    static int timerCounter = 0; 
    timerCounter++; 
    labelA.text = [NSString stringWithFormat:@"%d", timerCounter]; 
} 

가 더 이상 필요하지 않을 때 타이머 나중에 무효로하는 것을 잊지 마십시오.

+0

THanks ole Begemann –