1

我想经常更新标签的文本,但它似乎不起作用。

for (int i = 0; i <100; i++)
{
 mylabel.text =[NSString stringWithFormat:@"%d", i];
}

有没有人建议我一个想法?我想。也许,我们应该在多个线程中更新文本标签。

4

3 回答 3

1

手表是最好的例子,希望它对你有帮助......

int second,minute;    //set second = 0 and minute = 0
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];

- (void)updateCounter:(NSTimer *)theTimer {
    second++;

    if(second < 60)
        {
        if (second < 10) 
            {
            timerLbl.text = [NSString stringWithFormat:@"00:0%d", second];
            }
        else
            {
            timerLbl.text = [NSString stringWithFormat:@"00:%d",second];
            }
        }
    else
        {
        minute = second / 60;
        int sec = second % 60;

        if (minute < 10 && sec < 10) 
            {
            timerLbl.text = [NSString stringWithFormat:@"0%d:0%d", minute, sec];
            }
        if(minute < 10 && sec >= 10)
            {
            timerLbl.text = [NSString stringWithFormat:@"0%d:%d", minute, sec];
            }
        if (minute >= 10 && sec < 10) 
            {
            timerLbl.text = [NSString stringWithFormat:@"%d:0%d", minute, sec];
            }
        if(minute >= 10 && sec >= 10)
            {
            timerLbl.text = [NSString stringWithFormat:@"%d:%d", minute, sec];
            }
        }
}
于 2013-04-03T09:19:25.950 回答
1
Add below code in viewDidLoad
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5f
                                     target:self
                                   selector:@selector(showTime)
                                   userInfo:NULL
                                    repeats:YES];

- (void)showTime
{
    NSDate *now=[NSDate date];
    NSDateFormatter *dateFormatter=[NSDateFormatter new];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    timeLabel.text=[dateFormatter stringFromDate:now];
}

希望这个答案能帮到你......

于 2013-04-03T09:34:37.840 回答
1

使用 NSTimer 作为定期间隔并调用打印标签的方法让假设

[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:Yes];

在目标方法中在文本上打印标签

-(void)targetMethod:(NSTimer *)timer{
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@" HH:MM:SS"];
NSString* str = [formatter stringFromDate:date];
label.text = str;
}

试试这个它肯定会工作

于 2013-06-27T09:50:08.730 回答