我有一些功能,我想在它们工作时显示progreeeHUD
我想出了两个显示和隐藏hud的函数
func showHud() {
let hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
和
func hideHud() {
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
我试过这样做
viewDidLoad
showHud()
func1()
func2()
func3()
func4()
showHud()
但这不起作用,hud 没有显示
我也试过了
@IBAction func goToGame(sender: UIButton) {
let progressHUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
progressHUD.labelText = "Loading..."
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)) {
self.JSONRequest()
self.loadFromDb()
self.checkForDupl()
self.saveToDb()
dispatch_async(dispatch_get_main_queue()) {
progressHUD.hide(true)
self.performSegueWithIdentifier("toGameSegue", sender: nil)
}
}
这里的问题是必须在“toGameSegue”之后显示的 viewController 不等待 JSONRequest 和后续操作的完成,因此应用程序崩溃,因为它没有所需的数据
最佳答案
无阻塞
试试这个可见:
let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
loadingNotification.mode = MBProgressHUDMode.Indeterminate
loadingNotification.labelText = "Loading"
关闭 ProgressHUD:
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
就您而言:
@IBAction func goToGame(sender: UIButton) {
let progressHUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
progressHUD.labelText = "Loading..."
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)) {
self.JSONRequest()
dispatch_async(dispatch_get_main_queue()) {
}
}
func JSONRequest()
{
//write it to success block
self.loadFromDb()
self.checkForDupl()
self.saveToDb()
progressHUD. hide (true)
self.performSegueWithIdentifier("toGameSegue", sender: nil)
}
关于ios - Swift - 如何正确使用 MBProgressHUD.h?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32117179/