使用块时UI问题

时间:2014-04-03 09:03:39

标签: objective-c objective-c-blocks mbprogresshud

我正在使用MBProgressHUD在我的应用上显示加载指示器。当我想在UI上更改某些内容或我在块内打开一个新屏幕时,我只看到一个空白的白色屏幕。有谁知道我的代码中缺少什么?

-(void)doSomething:(id)sender
{
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];

HUD.delegate = self;
HUD.labelText = @"Please wait";
[HUD showAnimated:YES whileExecutingBlock:^{
NSDictionary* dictReturn = [ServerApi getItemDetails::itemId userId:userId;
NewScreenController* vc = [[NewScreenController alloc]init];
[self presentViewController:vc animated:YES completion:nil];
}];    
}

1 个答案:

答案 0 :(得分:2)

MBProgressHUD不会在主线程中执行该块。这就是为什么你根本不应该改变UI的原因。

您应该使用completionBlock代替方法。

- (void)doSomething:(id)sender {
     HUD = [[MBProgressHUD alloc] initWithView:self.view];
     [self.view addSubview:HUD];

     HUD.delegate = self;
     HUD.labelText = @"Please wait";

     dispatch_block_t executionBlock = ^{
        self.dictReturn = [ServerApi getItemDetails:itemId userId:userId];
     };

     void (^completionBlock)() = ^{
         NewScreenController *vc = [[NewScreenController alloc] init];
         [self presentViewController:vc animated:YES completion:nil];
     };

     [HUD showAnimated:YES
   whileExecutingBlock:executionBlock
       completionBlock:completionBlock];
}