2011-06-24 30 views
-2

我試圖等待秒..iPhone定時器 - 如何等待秒?

for (int i = 0; i < 3; i++) { 
    // something A 
    ... 

    // wating 
    [NSTimer scheduledTimerWithTimeInterval:10 
            target:self 
            selector:@selector(endTimer:) 
            userInfo:nil 
            repeats:YES]; 

    // something B 

    ... 
} 


- (void)endTimer:(NSTimer *)timer { 
    NSLog(@"end timer"); 
    [timer invalidate]; 
} 

我想 'A - >等待 - > B'

但不要等待...

甲 - >乙 - > A→B→A→B→結束計時器,結束計時器,結束計時器

有什麼辦法嗎?

好日子

+0

如果你不介意我說的話,你應該用更好的英語來更改你的問題,這很難理解你所要求的。 – chewy

+0

對不起。我不會說英語......我正在學習英語。 – jPlus

+1

好吧,你明白「接受答案」是什麼意思? – chewy

回答

1

試試這個,

-(void)callA{ 
    //Do your thing... 
} 
-(void)callB{ 
    //Do your thing... 
} 
-(void)callFunction{ 
    count++; 
    if(count<3){ 
    [self performSelector:@select(callA) withObject:nil]; 
    [NSThread sleepForTimeInterval:3.0]; 
    [self callB]; 
    } 
    else{ 
     [timer invalidate]; 
    } 
} 

現在,創建主要功能從您要撥打的計時器以上功能。

timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(callFunction) userInfo:nil repeats:YES]; 
+0

謝謝賈利亞! – jPlus

0

即使您的問題沒有表述很好,

// set myCount somewhere as an INT with value 0 
// int myCount = 0; 

-(void)callA 
{ 
    if (myCount < 3){ 
     [self performSelector:@selector(callB) withObject:nil afterDelay:1.0]; 
    } 
} 


-(void)callB 
{ 
    myCount +=1; 
    [self performSelector:@selector(callA) withObject:nil afterDelay:1.0]; 

} 
+0

謝謝師世! – jPlus