Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
635 views
in Technique[技术] by (71.8m points)

objective c - Updating UILabel in a loop?

I want the screen to display the number on each iteration for the following example code?? This code shows the last iteration number regardless of using a pause...

for(int i = 0; i<10; i++){
    [lbl setText:[NSString stringWithFormat:@"%d", i]];
}

I have updates coming across TCP Sockets in a while loop... only the last update gets written to the screen... and it is slow so I should see all the updates.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should set a timer to handle the update of your label. Right now the whole loop is happening in a fraction of a second.

NSTimer * aTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimerLabel) userInfo:nil repeats:YES];

-(void)updateTimerLabel {
    static int i = 0;

    [lbl setText:[NSString stringWithFormat:@"%d", i++];
}

This way your label should get updated once every second.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...