简体   繁体   中英

Cannot update UI on main thread

I am trying to update the text in a UILabel but I can't get it to change.

The update is triggered by one of two buttons. The first button works fine, here is its event handler:

- (void)laterPressedForLocation {
    self.locationLabel.text = @"Location off";
    // works as expected...
}

The second button will not update the text no matter what I try, here is its event handler:

// ask for location permission....
- (void)acceptPressedForLocation {
    _clLocManager = [[CLLocationManager alloc] init];
    _clLocManager.delegate = self;
   if ([_clLocManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
       [_clLocManager requestWhenInUseAuthorization];
}

// called when location permissions change
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

   dispatch_async(dispatch_get_main_queue(), ^{
      self.locationLabel.text = @"Location permission updated";
      // always runs, but does not work
   });
}

If I put a break point or a NSLog statement where the text change is, I see that the code is executed and the block completes - but the text never actually changes.

Note that this UILabel isn't changed anywhere else in the code.

How can I force the UI to update?

I tried delaying the update like this:

[self performSelector:@selector(updateText) withObject:nil afterDelay:0.01f];

but still nothing happens.

UPDATE

I found another similar case so I know it has nothing to do with the location manager - something is blocking self from performing updates..

The parent view has a button which calls removeFromSuperview on the view with the stubborn UILabel, and when I push that button, nothing happens and the UIView does not go anywhere. All my other views are still working fine - what would cause one UIView to stop responding to UI updates?

明白了。UIView初始化代码被第二次触发,并且在我要更改的视图前面有一个重复的视图,从而使所做的更改似乎无效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM