Search code examples
iosmbprogresshud

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!


Solution

  • 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.