2012-12-05 51 views
0

正確更新UIProgressView我有LoadViewController類和開始我把這:如何在Objective-C

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 

dispatch_async(kBgQueue, ^{ 

    NSData *dataOfURL = [NSData dataWithContentsOfURL:kRSSURL]; 

    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:dataOfURL waitUntilDone:YES]; 
}); 
} 

哪裏kbgQueue

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 

在主線程中執行fetchedData:

- (void)fetchedData:(NSData *)responseData { 

XMLParser *parser = [[XMLParser alloc] initWithData:responseData]; 
parser.delegate = self; 

if ([parser parse]) { 

    // if parsed. 
    if (!data) { 

     data = [[NSMutableArray alloc] init]; 
    } 
    else { 

     [data removeAllObjects]; 
    } 

    data = [parser getParsedData]; 

    ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"]; 
    viewController.array = data; 

    [self presentViewController:viewController animated:YES completion:^{}]; 
    //[self reloadData]; 
} 
else { 

    NSLog(@"False."); 
} 

} 

正如你看到的我用我自己的XMLParser輔助類,其中i獲取所有數據。當一個數據被下載我的代表呼籲[self.delegate passProgress:fetchedItems/20.0f];LoadViewController是這樣的:

-(void)passProgress:(float)progress { 

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self.progressView setProgress:progress]; 
    NSLog(@"Updated!"); 
}); 
} 

但問題是:在下載一個20個數據項後,progressView不會更新。哪裏有問題?

+1

下載數據時最好使用'NSURLConnection',因爲這樣你就可以處理包含字節數據並更新進度以顯示真正的數據百分比,看看偉大的['AFNetworking'](https:// github的.com/AFNetworking/AFNetworking)。 – rckoenes

+0

@rckoenes - 謝謝。我會檢查的。 –

回答

0

我不知道是不是正確的,但我在LoadViewController改變從perfromOnMainThread:withObject:waitUntilDone:perfromInBackground:withObject:,但它的作品。