2012-08-30 21 views
0

我想爲我的方法添加完成回調,以便HUD的進度知道它已完成。爲MBProgressHUD添加完成回調

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

回答

3

HUD情況下,你可以使用它的delegate功能hudWasHidden會在您使用HUD這樣的 -

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

和如果你想知道如何使用callbacksobjective c然後按照這個帖子 -

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

完成回調方法 -

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

    completion(); 
} 
+0

使用showWhileExecuting不增加HUD雖然正確內的進度條的進度?我認爲這是回調部分的作用? – StuartM

+0

您是否在談論HUD的進度視圖進度,然後您可以通過showWhileExecuting增加進度。 – saadnib

+0

是的,如果我只使用showWhileExecuting(方法),那麼HUD中的進度條的AnnularDeterminate模式的進度並沒有增加......我推測使用上述樣式與回調來完成將更新過程。 – StuartM