0

我正在使用 xCode 4.3.2。在这个项目中,当我通过单击按钮调用“reloadXMLdata”时,它没有显示activityIndi​​cator,在加载时它看起来像“挂起”,几秒钟后它会填充数据。如何在加载时显示 activityIndi​​cator?你能帮我解决这个问题吗?

-(void)loadXML
{
    NSString *urlAddress = [NSString stringWithFormat:@"my_xml_url"];    
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [xmlParser setDelegate:self];
    BOOL success = [xmlParser parse];

    if(success)
    {
        [self.activityIndicator setHidden:TRUE];
        [self.activityIndicator stopAnimating];
        [dataTable reloadData];
    }
    else
    NSLog(@"Error!!!");
}


-(IBAction)reloadXMLdata:(id) sender
{
    [self.activityIndicator setHidden:FALSE];
    [self.activityIndicator startAnimating];
    [self loadXML];
}
4

1 回答 1

0

问题是您在调用时阻塞了 UI,loadXML并且活动指示器没有机会显示。所以尝试像这样使用GCD

-(IBAction)reloadXMLdata:(id) sender
{
    [self.activityIndicator setHidden:FALSE];
    [self.activityIndicator startAnimating];
    [self loadXML];
}

-(void)loadXML
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *urlAddress = [NSString stringWithFormat:@"my_xml_url"];    
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
        [xmlParser setDelegate:self];
        BOOL success = [xmlParser parse];

        if(success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.activityIndicator setHidden:TRUE];
                [self.activityIndicator stopAnimating];
                [dataTable reloadData];
            });
        } else {
            NSLog(@"Error!!!");
        }
    });
}
于 2012-04-21T10:18:51.540 回答