2012-07-20 55 views
0

我想在我的应用程序中显示HUD的混合,所以例如,当用户点击“登录”时,我想让我的HUD显示微调器说“登录...” ,然后更改为“登录!”的复选标记图像,然后隐藏。我想这个使用下面的代码来完成:这里MBProgressView睡眠混合视图

MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
HUD.labelText = @"Logging in"; 
\\Do network stuff here, synchronously (because logging in should be synchronous) 

\\ Then upon success do: 
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark.png"]]; 
HUD.mode = MBProgressHUDModeCustomView; 
HUD.labelText = @"Logged in!"; 
sleep(2); 
[MBProgressHUD hideHUDForView:self.view animated:YES]; 

问题,就是sleep(2)被应用到最初的微调,而不是对号HUD。所以微调器显示的时间更长,并且复选标记在瞬间消失后消失。我怎样才能做到这一点,以便勾选在HUD隐藏之前保持更长时间?

谢谢!

回答

1

作为最佳实践,不要使用睡眠。尝试使用“performSelector:withObject:afterDelay”方法。创建一个方法,它可以在您选择的预定义延迟之后执行并调用该方法。不要忘记你正在处理UI,所以确保你在主线程中调用它。

+0

酷!啊,我的坏,这是一个重复的问题措辞有点不同。由stavash建议的代码在这里:http://stackoverflow.com/questions/7308922/mbprogresshub-mixed-view?rq=1 – quantum 2012-07-20 15:17:24

0

我会创建两个HUD。第一个是“等待”部分,第二个是成功。您的网络任务之前启动loadingHUD,并隐藏它,当你完成:

MBProgressHUD *loadingHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
loadingHUD.mode = MBProgressHUDModeIndeterminate; 
loadingHUD.labelText = @"Please wait..."; 
loadingHUD.detailsLabelText = @"Connection in progress"; 
[loadingHUD show:YES]; 
// Do the network stuff here 
[loadingHUD hide:YES]; 

权后,通知的成功,因为你希望它创建successHUD,并延迟后隐藏:

MBProgressHUD *successHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
successHUD.mode = MBProgressHUDModeCustomView; 
successHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark.png"]]; 
successHUD.labelText = @"Logged in !"; 
[successHUD show:YES]; 
[successHUD hide:YES afterDelay:2.0]; 

您的成功HUD将显示2秒钟,并自动隐藏。

这就是我总是使用MBProgressHUD的方式。