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不會更新。哪裏有問題?
下載數據時最好使用'NSURLConnection',因爲這樣你就可以處理包含字節數據並更新進度以顯示真正的數據百分比,看看偉大的['AFNetworking'](https:// github的.com/AFNetworking/AFNetworking)。 – rckoenes
@rckoenes - 謝謝。我會檢查的。 –