Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3312262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:58:27+00:00 2026-05-17T21:58:27+00:00

In my program, I have a series of UIButtons in an array that I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:58:27+00:00Added an answer on May 17, 2026 at 9:58 pm

    Yes, you could used :

    • NSTimer
    • or just performSelector:withObject:afterDelay: of any NSObject subclass.
      You can not use sleep or loop in main thread. It will block the HMI.

    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;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of big excel files that work like a program, but
In my program, I have a series of if-else statements that look like so:
In my program I have one array with 25 double values 0.04 When I
I have a series of shapes (Approx. 50), that each have 5 points, and
in a C program I have an long* that I want to serialize (thus
I have a program that spits out both standard error and standard out, and
I have a program that creates a Windows user account using the NetUserAdd() API
I have a program that uses the mt19937 random number generator from boost::random. I
I have a program that spits out an Excel workbook in Excel 2003 XML
I have a series of large text files (up to 1 gig) that are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.