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

标签 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)

        }
    }

这里的问题是必须在“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/

相关文章:

ios - 如何在 iOS8/Swift 中使用带有单独 Storyboard的 UITabBar?

ios - MBProgressHUD - 2 个连续的消息重叠

ios - 在 iOS 6 中,UIViewController 能否支持比其父级更多的界面方向?

ios - 如何计算文件夹的大小?

ios - 该应用程序在 :setRefreshInterval 中引用了非公共(public)选择器

xcode - 存档时 MBProgressHud Swift 编译错误

iphone - 在 2 个 UITableViews 应用程序卡住之间交替

ios - 将 nsarray 的值添加到另一个

ios - 在我的 iOS 应用程序中与外部配件通信以播放流中的视频

swift - 使用 ApplicationShortCut SIGTRAP 时崩溃