I am trying to create two functions. One to start a progressHUD and one to stop the hud. I am creating this inside an extension of UIViewController. The functions are below:
func startProgressHUD() {
let spinningActivity = MBProgressHUD.showAdded(to: self.view, animated: true)
spinningActivity.label.text = "Loading"
}
func stopProgressHUD() {
DispatchQueue.main.async {
MBProgressHUD.hide(for: self.view, animated: true)
}
}
The first function works fine when called -> the HUD starts.
However, when I call the stop function, the HUD never stops. According to the description, the hide function I call inside my stop function hides the top-most HUD in the view. I only have one HUD open in the view, but the HUD never stops.
If I use:
func stopProgressHUD() {
DispatchQueue.main.async {
MBProgressHUD.hideAllHUDs(for: self.view, animated: true)
}
}
I have no problem, but the hideAllHUDs function is deprecated -> the warning states, 'store references when using more than one HUD per view'.
How do I get the HUD to stop?
Declare your
spinningActivityas a global variable in yourViewController. Anytime when a HUD is needed you can doself.spinningActivity = MBProgressHUD.showAdded(to: self.view, animated: true). And when you don't need it anymore,self.spinningActivity?.hide()