Таймер iPhone - Как ждать секунды? - PullRequest
0 голосов
/ 24 июня 2011

Я пытаюсь подождать несколько секунд ..

 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 -> A -> B -> таймер завершения, таймер завершения, таймер окончания

Есть ли способ?

добрый день

Ответы [ 2 ]

1 голос
/ 24 июня 2011

попробуйте это,

-(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 голосов
/ 24 июня 2011

Даже если ваш вопрос сформулирован плохо,

// 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];

}
...