2013-06-20 18 views
6

Belirli bir süre için periyodik bir görev yapmak için scheduleTimerWithTimeInterval kullanmak istiyorum bir saat söyleyelim. ama kodumu nasıl uygularım. İşte benim kodum:ios scheduleTimerWithTimeInterval süre için

timer = [NSTimer scheduledTimerWithTimeInterval: 0.1 
              target: self 
             selector: @selector(doSomething:) 
             userInfo: nil 
             repeats: YES]; 
+1

Kodunuz iyi görünüyor. "DoSomething:" yöntemini kırın ve aranıyor mu bakın. –

cevap

5

Bir dakika için farz edelim. İşte

senaryosu mevcut,

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:@selector(countDown) userInfo:nil repeats:YES]; 
remainingCounts = 60; // int remainingCounts ;... 

Yöntem her saniye için çağrıldığını.

-(void)countDown { 
NSLog(@"%d", remainingCounts); 
    // Do Your stuff...... 
    if (--remainingCounts == 0) { 
    //[self dismissViewControllerAnimated:YES completion:nil]; 
    [timer invalidate]; 
} 

}