为MBProgressHUD添加完成回调

时间:2012-08-30 12:01:16

标签: ios ios5 uiviewcontroller mbprogresshud

我想在我的方法上添加一个完成回调,以便HUD的进度知道它已经完成。

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.labelText = @"Loading";
[self doSomethingInBackgroundWithProgressCallback:^(float progress) {
hud.progress = progress;
} completionCallback:^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];

我需要添加到我的方法中以确认它是完整的,还是从上面触发此completionCallback?

在这种情况下,我的方法可以是任何例子:

-(void)doSomethignInBackgroundWithProgressCallback {
sleep(100);
}    

1 个答案:

答案 0 :(得分:3)

如果是HUD,您可以使用delegate函数hudWasHidden,当您使用HUD这样时,

[HUD showWhileExecuting:@selector(your_function) onTarget:self withObject:nil animated:YES];

如果您想知道如何在callbacks中使用objective c,请按照此帖发布 -

http://stackoverflow.com/questions/1015608/how-to-perform-callbacks-in-objective-c

完成回调方法 -

- (void) doSomethingInBackground:(void (^) (void)) completion
{
    // do your job here

    completion();
}