2011-06-24 29 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