如何动态更改标签文本

时间:2012-06-17 14:42:46

标签: iphone objective-c ipad

我想制作一个“下载”uilabel,当我调用方法“更新”时,其标签文本会动态改变:

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;
}

但标签文字留有文字“下载...”,我想知道如何实现我的目的?

2 个答案:

答案 0 :(得分:3)

实施这个:

- (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;
}

答案 1 :(得分:1)

这样做:

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++;
  }

}