views:

41

answers:

1

In my program, I have a series of UIButtons in an array that I would like its title to be shown each one at a time one by one in a timed sequence after executing a function that has a while loop.

I have an IBAction attached to the buttons and when touched will call another function that will do some operation and in the end, will change the UIButtons title in that array.

The problem is that it changed the whole buttons title simultaneously after executing that method but I want the title to be shown one by one in a timed sequence.

To illustrate clearly, here are my codes:

-(IBAction)touchedButton1:(id)sender
{
   [self calculateSteps:0];
}


-(void)calculateSteps:(NSUInteger)hole_index2
{

 index = hole_index2;

 NSNumber *tempNumber = [marblesArray objectAtIndex:index];  
 stones = [tempNumber intValue];

 [marblesArray replaceObjectAtIndex:index withObject:[NSNumber numberWithInt:0]];
 [[buttonsArray objectAtIndex:index] setTitle:[NSString stringWithFormat:@"%d", [NSNumber numberWithInt:0].intValue] forState:UIControlStateNormal];


 while(stones > 0) {  

  if (player == PLAYER_A && stones >= 1 && index == 6) { 

   NSNumber *tempNumber3 = [storeArray objectAtIndex:0];  
   NSUInteger tempInt3 = [tempNumber3 intValue]; 

   tempInt3++;

   [storeArray replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:tempInt3]]; 
   [buttonPlayerA setTitle:[NSString stringWithFormat:@"%d", [NSNumber numberWithInt:tempInt3].intValue] forState:UIControlStateNormal];

   stones--; 


   if (stones == 0) { // end in PLAYER A's store 

    NSLog(@"end in big hole player A");

    return;
   }
  } 

  if (player == PLAYER_B && stones >= 1 && index == 12) { 

   NSNumber *tempNumber4 = [storeArray objectAtIndex:1];  
   NSUInteger tempInt4 = [tempNumber4 intValue]; 

   tempInt4++;

   [storeArray replaceObjectAtIndex:1 withObject:[NSNumber numberWithInt:tempInt4]];
   [buttonPlayerB setTitle:[NSString stringWithFormat:@"%d", [NSNumber numberWithInt:tempInt4].intValue] forState:UIControlStateNormal];

   stones--;
   if (stones == 0) { // end in PLAYER B's store
    NSLog(@"end in big hole player B");
    return;
   }
  }



  index++; 
  if (index >= NUM_HOLES) index = 0; 

  NSNumber *tempNumber2 = [marblesArray objectAtIndex:index];  
  tempInt2 = [tempNumber2 intValue]; 
  tempInt2++;

  [marblesArray replaceObjectAtIndex:index withObject:[NSNumber numberWithInt:tempInt2]]; 
  NSLog(@"Number in marblesArray index: %d", [NSNumber numberWithInt:tempInt2].intValue);
  [[buttonsArray objectAtIndex:index] setTitle:[NSString stringWithFormat:@"%d", [NSNumber numberWithInt:tempInt2].intValue] forState:UIControlStateNormal];

  stones--; 
 }



}

So, I have tried to put NSTimer in the calculateSteps method and also in that while loop but couldn't get to work. I guess maybe the while loop function fast enough that it didn't get the chance to get NSTimer to work in time.

I know it could work if I use if timer == 1, or else if timer == 2, etc as the timer increase, each button associated with it will change after that interval. However, when I tried to use for (timer == 1; timer < stones; timer++) , it doesn't show the buttons title each one by one but simultaneously after it is done with the loop. Am I wrong with my logic?

Also, I've tried to put sleep(2) in the while loop also, it works for the NSLog(@"Number in marblesArray index:...") appearing each 2 seconds but still the buttons array title still shown simultaneously after while loop completed.

I'm not sure how can I get the buttons title in that array to be shown each one by one in a timed sequence. Have been trying for 2 days but couldn't get it to work. Appreciate if anyone out there who can help me. I don't know if it's my logic or if there's other function that can be used to solve this issue.

Thank you very much in advance.

A: 

Yes, you could used :

But I don't really understand your code and your explanation...

In the target selector of the timer, you could have a static int to count the number of ticks example (very simple):

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(onTimer) userInfo:nil repeats:NO];

-(void)onTimer{
  static int ticks = 0;
  ticks++;
  if (ticks==1) {
    //TODO
  } else if (ticks==2) {
    //TODO
  } else if (ticks==3) {
    //TODO
  } else {
    //TODO
    ticks=0;
  }
}
Benoît
I know you could use NSTimer but I don't know where to put it in the loop. If i try to put it, it won't work. I don't know which part you didn't understand. It's kinda simple, I just want to have title changes in a series of uibutton in a timed sequence. For example, button A change its title after 1 second, and then butoon B change its title after 2 seconds and it goes on etc. Right now, all of the buttons change its title after performing the calculateSteps method simultaneously.
Tom A.
You can add in the loop. You have to make your algorithm event mode
Benoît
Yeah i know how to do that with that code you gave. The problem is for each seconds, I have to manually insert code for each button to set its title. I have tried to do it with for (ticks == 0; ticks < 10, ticks++) , but it didn't trigger the ticks to increase timer by one OR maybe you can't do it with for loops, I just don't know.
Tom A.
give your code with NSTimer use
Benoît
for (numTimerTicks == 0; numTimerTicks < 10; numTimerTicks++) { [[buttonsArray objectAtIndex:numTimerTicks] setTitle:[NSString stringWithFormat:@"%d", numTimerTicks] forState:UIControlStateNormal]; }
Tom A.
shouldn't it be logically increase the timer by 1 second each time and then the button title for each in array also increase by 1 second.. ? isn't it the same like if, else if, else if etc.?
Tom A.
don't use a loop ! The loop is the timer with interval
Benoît
Ahh i see. I think I get what you mean. Basically, I have to work around my function around the nstimer right, like you said make the algorithm event mode.
Tom A.
Anyway, just wandering how do I make onTimer method execute depending on certain variables? Let say, if there need to have 7 buttons title to be changed, obviously I would have to set each button title in 1 second interval, what if i have 15 buttons title to be changed, how would i predefined the if, else if, else method? Shouldn't i be using for loop if I want to increase the timer by 1 second and would depends on other variables?
Tom A.
You can initialized an array of button and used this array in the Timer event
Benoît
Hey man, thanks a lot! I got it working! you're right about The Loop is the Timer with interval - very important key. I got confused with that. Thanks a lot!!!
Tom A.