quantum
quantum

Reputation: 1420

MBProgressView sleep for mixed views

I'm trying to show a mix of HUDs in my app, so for example, when a user taps "login", I want my HUD to display the spinner saying "logging in...", then change to a checkmark image saying "logged in!", then hide. I'm trying to accomplish this using the following code:

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

Problem here, is that the sleep(2) is being applied to the initial spinner, and not the checkmark HUD. So the spinner is showing for a longer period of time, and the checkmark disappears after a split second. How can I do it so that the checkmark stays there for a longer time before the HUD hides?

Thanks!

Upvotes: 0

Views: 326

Answers (2)

rdurand
rdurand

Reputation: 7410

I would create two HUDs. The first one for the "waiting" part, and the second one for the success. Start the loadingHUD before your network task, and hide it when you are done :

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

Right after that, to notify the success, create the successHUD as you want it, and hide it after delay :

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

Your success HUD will show up for 2 seconds, and hide automatically.

That's how I always use MBProgressHUD.

Upvotes: 0

Stavash
Stavash

Reputation: 14304

As best practice, don't use sleep. Try using the "performSelector:withObject:afterDelay" method. Create a method that does the

[MBProgressHUD hideHUDForView:self.view animated:YES];

action and call it after a predefined delay of your choice. Don't forget that you're handling UI so make sure you're calling this on the main thread.

Upvotes: 1

Related Questions