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:
- create a Method that handles your UI-related code and gets called within your callback method
- 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];
}
精彩评论