2012-06-17 83 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++; 
    } 

}