Swift - 如何正确使用MBProgressHUD.h?

时间:2015-08-20 11:35:44

标签: ios swift mbprogresshud

我有一些功能,我想在他们工作期间显示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)

        }
    }

这里的问题是viewController,必须在" toGameSegue"之后显示不等待JSONRequest的完成和跟随,因此应用程序崩溃,因为它没有所需的数据

1 个答案:

答案 0 :(得分:1)

没有阻止

尝试使用Visible:

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)
}