0

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];
}
4

1 回答 1

1

你可以这样做:

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

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

这使 UI 有机会通过在阻塞主线程之前将控制权返回给 runloop 来更新。根据您的任务,使用后台线程或 Grand Central Dispatch 可能是个好主意,这样 UI 的其余部分就不会阻塞,您可以为用户提供取消进程的选项(这是不可能的上面的简单方法)。

于 2011-01-13T03:18:34.850 回答