How to update UILabel immediately?

I am trying to create a UILabel that will inform the user about what is happening while he is waiting. However, UILabel always delays its text update until the system goes into standby mode.

Process:

[infoLine performSelectorOnMainThread:@selector(setText:) withObject:@"Calculating..." waitUntilDone:YES];
[distanceManager calc]; // Parses a XML and does some calculations
[infoLine performSelectorOnMainThread:@selector(setText:) withObject:@"Idle" waitUntilDone:YES];

Do not wait. Do you do this "immediately"?

+3
source share
4 answers

, waitUntilDone. setText, setNeedsDisplay , NSTimer , , 1 , /. , , , , switch ( chunk, execute chunk, index chunk index, exit), . . , ( 15 200 ).

+4

waitUntilDone , setText: , , .

, call -setNeedsDisplay , .

0

Here's a useful feature that I added to the subclass UIViewController. It executes the selector in the next run loop. It works, but do you think I should make NSTimer *timeran instance variable, since this method will probably be called more than once.

- (void)scheduleInNextRunloopSelector:(SEL)selector {
    NSDate *fireDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0.001]; // 1 ms
    NSTimer *timer = [[NSTimer alloc]
                      initWithFireDate:fireDate interval:0.0 target:self
                      selector:selector userInfo:nil repeats:NO];
    [fireDate release];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [timer release];
}
0
source

Use performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval):

self.infoLine.text = @"Calculating...";
[self performSelector:@selector(likeABoss) withObject:nil afterDelay:0.001];
//...
-(void) likeABoss {
    // hard work here
    self.infoLine.text = @"Idle";
}
0
source

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


All Articles