Unable to update text in UILabel

I created my ViewController from a UIViewController, where it adds UILabel as; @property (non-atomic, persistent) UILabel * progressLabel;

I initialize this label in loadView

- (void)loadView {

 // Set main view to View Controller
 UIView* localView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] ];
 self.view = localView;
 [localView release];

 // Set progress label
 self.progressLabel =  [[UILabel alloc] initWithFrame:CGRectMake(0, 420, 320, 20) ];
 [self.progressLabel setFont:[UIFont fontWithName:@"Georgia" size:12]]; 
 [self.progressLabel setTextColor:[UIColor whiteColor]];
 [self.progressLabel setBackgroundColor:[UIColor blackColor]];
 [self.view addSubview:self.progressLabel];

}

And I have a method that should update the label text

-(void) doSomethig {
 for(int i = 0; i < 10; i++) {
  NSString* str = [NSString stringWithFormat:@"%d", i];
  [self.progressLabel setText:str];
  [self.progressLabel.superview setNeedsDisplay];
  NSLog(@"%@", self.progressLabel.text);
  sleep(1);
 }
}

Why does this code not update the progressLabel.text property? However, in the debuger console, I see the following text:

2010-02-26 14:21:55.707 iLabelUpdate[6272:207] 0
2010-02-26 14:21:56.708 iLabelUpdate[6272:207] 1
2010-02-26 14:21:57.708 iLabelUpdate[6272:207] 2
2010-02-26 14:21:58.709 iLabelUpdate[6272:207] 3
2010-02-26 14:21:59.709 iLabelUpdate[6272:207] 4
2010-02-26 14:22:00.710 iLabelUpdate[6272:207] 5
2010-02-26 14:22:01.710 iLabelUpdate[6272:207] 6
2010-02-26 14:22:02.711 iLabelUpdate[6272:207] 7
2010-02-26 14:22:03.711 iLabelUpdate[6272:207] 8
2010-02-26 14:22:04.711 iLabelUpdate[6272:207] 9

And when the cycle is over, do I see the “9” mark in the progress mark?

+3
source share
3 answers

Running a loop updates the user interface. You can try calling runloop in a for loop:

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
+3
source

, . , .

NSTimer, -timerDidFire: .

+2

. text. , , . .

NSTimer 1 . .

+1

Source: https://habr.com/ru/post/1734544/


All Articles