开发者

iPhone UIView not updating instantly

开发者 https://www.devze.com 2023-01-06 12:03 出处:网络
My application has a button when clicked it is disabled, an activity indicator displayed and a background task is executed. When this task is completed a callback updates the interface by enabling the

My application has a button when clicked it is disabled, an activity indicator displayed and a background task is executed. When this task is completed a callback updates the interface by enabling the button and removing the activity indicator. The problem I having is the task is completing the callback function is executed but for a period of time the activity monitor remains on the screen, the bu开发者_如何学JAVAtton looks like it is disabled but it is possible to click it again. Can anyone tell me where I am going wrong?

Thanks very much!


Could it be that the callback-method is being executed in a separate thread? I'm asking, because any calls that have impact on a view should be performed on the main thread.

The problem might be solved by doing the following:

  1. create a Method that handles your UI-related code and gets called within your callback method
  2. the UI-related code has to be performed on the main thread

It could look a little bit like this:

//gets called asynchronously when your operation has completed
-(void)myCallbackHandler {
   [self performSelectorOnMainThread:@selector(updateUI) withObject:nil 
                                                      waitUntilDone:NO];
}

-(void)updateUI {
    [myActivityIndicatorView stopAnimating];
    [myButton setEnabled:YES];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消