itenyh
itenyh

Reputation: 1939

How to change the label text dynamically

I want to make a "downloading" uilabel whose label text will change dynamically when I call the method "updating":

self.checkingArray = [NSArray arrayWithObjects:@"download", @"download.", @"download..", @"download...",
                 nil];

- (void) updating
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changeText) userInfo:nil repeats:YES];
}

- (void) changeText
{
    checkLabel.text = [self.checkingArray objectAtIndex:curCheckingState];

    self.curCheckingState++;

    if (self.curCheckingState >= 4) {
    self.curCheckingState = 0;
}

But the label text stay with the text "download...", I want to know how to achieve my purpose?

Upvotes: 1

Views: 631

Answers (2)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Do this:

self.checkingArray = [NSArray arrayWithObjects:@"download", @"download.", @"download..", @"download...",
             nil];
self.curCheckingState = 0;


- (void) updating
{
 //change timer time interval if needed
 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeText) userInfo:nil repeats:YES];
}

- (void) changeText
{
  checkLabel.text = [self.checkingArray objectAtIndex:self.curCheckingState];

  if (self.curCheckingState == 3) //depending on [array count]-1
  {
    self.curCheckingState = 0;
  }
  else
  {  
    self.curCheckingState++;
  }

}

Upvotes: 1

trumpetlicks
trumpetlicks

Reputation: 7065

Implement this:

- (void) updating
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeText) userInfo:nil repeats:YES];
}

- (void) changeText
{
    static unsigned char state = 0;

    checkLabel.text = [self.checkingArray objectAtIndex:state];

    if(state < 3) state++;
    else state = 0;
}

Upvotes: 3

Related Questions