开发者

Is there any solution about UIActivityIndicatorView in synchronous process?

开发者 https://www.devze.com 2023-02-03 19:01 出处:网络
In a method, i start the UIActivityIndicatorView to start. and then use NSXMLParser to get the node information from the XML with synchronization.

In a method, i start the UIActivityIndicatorView to start. and then use NSXMLParser to get the node information from the XML with synchronization. After finished parse, i want stop the UIActivityIndicatorView. My propose is to appear the UIActivityIndicatorView when parse the XML, but it doesn't work. Any ideas? Thanks.

- (void)ButtonTouch{

[activityIndicator startAnimating];
    /*get the login result*/
    loginXMLDealer *loginxmldealer = [[loginXMLDealer alloc] init];
    loginxmldealer.username = usernameField.text;
    loginxmldealer.password = passwordField.text;
    [loginxmldealer loginResult];
    [activityIndicator stopAnimating];
    if ([loginxmldealer.rspCode isEqualToString: @"0001"]) {

        UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please check your passport." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
        [errorAlert release];
    }
    else {
 开发者_运维知识库       [self presentModalViewController:self.dataMainController animated:YES]; 

    }
    [loginxmldealer release];
}


You could do something like this:

- (void)buttonAction
{
  [activityIndicator startAnimating];
  [self performSelector:@selector(doWork) withObject:nil afterDelay:0.0];
}

- (void)doWork
{
  //Do your xml parsing here...
}

This gives the UI the chance to update by returning control to the runloop before you block the main thread. Depending on your task, it might be a good idea to use a background thread or Grand Central Dispatch instead, so that the rest of the UI doesn't block and you can give a user the option to cancel the process (which is impossible with the simple approach above).

0

精彩评论

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