将MBProgressHUD用于多个调度

时间:2012-03-29 19:50:25

标签: objective-c ios5 mbprogresshud

在处理线程和主线程上获取HUD标签的最佳方法是什么?

[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;

它将显示连接但不会显示正在处理或下载。

2 个答案:

答案 0 :(得分:1)

由于Objective-C区分大小写,因此您在此处有两个MBProgressHUD实例:

    您使用HUD创建的
  • [[MBProgressHUD alloc] initWithView:self.view];,而不是添加到视图,但无法显示(最初隐藏)
  • 您创建的
  • hud,添加到视图并立即显示方便构造函数[MBProgressHUD showHUDAddedTo:self.view animated:YES];

这实际上意味着HUD隐藏在整个代码中,您在其上设置的任何属性更改都不会显示(处理和下载),而hud是可见的,并显示唯一的文本设置它(Connecting1)。

您的代码中还有一个错误,它涉及在后台线程中创建一个视图(hud MBProgressHUD实例)。一般的经验法则是仅修改主线程中的视图。设置hud的文本(和一些其他属性)是一个值得注意的例外,因为MBProgressHUD在这里做了一些KVO技巧,以确保线程安全。

此外,您应该知道,即使您修复了上述错误,您也可以将文本设置为“登录”(或显示警报)并立即隐藏HUD,这意味着文本只会非常简短地显示。下载完成后,您可能不想隐藏HUD。 activitySpinner也有类似的问题。

总而言之,你可能会尝试这样的事情(从头顶写下):

[activitySpinner startAnimating];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Connecting1";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    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"];
                // Download synchronosly here? 
                dispatch_async(dispatch_get_main_queue(), ^{
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    [activitySpinner stopAnimating];
                });
            });

            [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];
        }
    });
});

答案 1 :(得分:0)

实际上在设备上测试后,我发现它确实显示了处理和下载。它只是没有在模拟器中显示它。我想因为它使用的是计算机处理器,所以它发生得非常快。