在处理线程和主线程上,获得标签更改为HUD
的最佳方式是什么?多个派发使用MBProgressHUD
[activitySpinner startAnimating];
//[HUD setLabelText:@"Connecting"];
//[HUD showUsingAnimation:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Connecting1";
NSString *url = [NSString stringWithFormat:@"my URL", [dataObj.pListData objectForKey:@"API_BASE_URL"], userField.text, passwordField.text];
NSLog(@"Login: %@",url);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
[HUD setLabelText:@"Processing"];
dispatch_async(dispatch_get_main_queue(), ^{
if ([json objectForKey:@"authToken"] != nil) {
[HUD setLabelText:@"Logging In"];
NSLog(@"Found authtoken %@", [json objectForKey:@"authToken"]);
[dataObj setAuthToken:[json objectForKey:@"authToken"]];
[dataObj setLocationId:[json objectForKey:@"c_id"]];
[dataObj setStaffId:[[json objectForKey:@"staffOrRoomsId"] integerValue]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[HUD setLabelText:@"Downloading"];
});
[self getAllData];
[self performSegueWithIdentifier:@"segueToRootController" sender:self];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:[json objectForKey:@"friendlyErrors"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
alert = nil;
}
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
[activitySpinner stopAnimating];
});
我试过上面的,因为如果我在主线程上运行标签更改,它将不会改变,直到所有处理完成。
在我viewWillAppear
我设置
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
它会显示连接,但不会显示处理或下载。
谢谢,我会确保我没有创建任何线程。自从这篇文章我修改了代码,所以我会确定的。 – Bot 2012-04-23 18:29:36