2012-06-17 170 views
1

我要让其标签的文字时,我调用该方法“更新”将动态地改变为“下载”的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; 
} 

但标签文本留在文本“下载...“,我想知道如何达到我的目的?

+1

尝试:'checkLabel.text = [self.checkingArray objectAtIndex:self.curCheckingState]' – Pfitz

+0

增加0.5到2,看看标签是否变化也确保self.curCheckingState = 0 –

+0

使用活动指标与uapatarance –

回答

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

}