【问题标题】:How to use MBProgressHUD with swift如何快速使用 MBProgressHUD
【发布时间】:2014-11-12 07:40:05
【问题描述】:

这是我的代码,但它显示了进度。这段代码有什么错误吗?请提供一些解决此问题的想法,或提供一些与此相关的链接。

class Approval: UIViewController {

var hud: MBProgressHUD = MBProgressHUD()

override func viewDidLoad() {
    super.viewDidLoad()

    fetchData()
}
   func fetchData(){
      hud.show(true)
      // doing some http request
      dispatch_async(dispatch_get_main_queue()) {
         hud.hide(true)          
      }
   }

}

【问题讨论】:

  • 异步执行你的http请求

标签: ios iphone swift ios8


【解决方案1】:

更新答案:

let loadingNotification = MBProgressHUD.showAdded(to: view, animated: true)
loadingNotification.mode = MBProgressHUDMode.indeterminate
loadingNotification.label.text = "Loading"

关闭 ProgressHUD:

MBProgressHUD.hideAllHUDs(for: view, animated: true)

【讨论】:

  • hideAllHUDs 已弃用。当每个视图使用多个 HUD 时,我们应该存储引用。
  • 在 swift 中试用这个适用于 iOS 的 HUD 库 github.com/shubh10/JustHUD
  • @EricDXS 我们能否也设置 MBProgressHUD 提供的成功消息“Done”。
【解决方案2】:

您也可以尝试这种方法,让其他活动保持在后台运行,让 UI 保持响应,为用户提供更好的体验。这是使用 MBProgressHUD 的预期/推荐方法。

let progressHUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
progressHUD.labelText = "Loading..."

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)) {

    // ...Run some task in the background here...

    dispatch_async(dispatch_get_main_queue()) {
        progressHUD.hide(true)

        // ...Run something once we're done with the background task...
    }
}

【讨论】:

    【解决方案3】:

    Swift 3 扩展

    import Foundation
    import MBProgressHUD
    import QuartzCore
    
    extension UITableViewController {
        func showHudForTable(_ message: String) {
            let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
            hud.label.text = message
            hud.isUserInteractionEnabled = false
            hud.layer.zPosition = 2
            self.tableView.layer.zPosition = 1
        }
    }
    
    extension UIViewController {
        func showHud(_ message: String) {
            let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
            hud.label.text = message
            hud.isUserInteractionEnabled = false
        }
    
        func hideHUD() {
            MBProgressHUD.hide(for: self.view, animated: true)
        }
    }
    
    // Use extensions
    

    【讨论】:

      【解决方案4】:

      创建易于使用并贯穿整个应用程序的扩展程序

      extension UIViewController {
      
          func showHUD(progressLabel:String){
              DispatchQueue.main.async{
                  let progressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
                  progressHUD.label.text = progressLabel
              }
          }
      
          func dismissHUD(isAnimated:Bool) {
              DispatchQueue.main.async{
                  MBProgressHUD.hide(for: self.view, animated: isAnimated)
              }
          }
      }
      

      用法:

      1. SHOW - self.showHUD(progressLabel: "Loading...")

      2。隐藏 - self.dismissHUD(isAnimated: true)

      【讨论】:

      • 易于实施。谢谢
      【解决方案5】:

      通过下面的代码

      class ViewController: UIViewController, MBProgressHUDDelegate {
          var hud : MBProgressHUD = MBProgressHUD()
          func fetchData() {
              hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
              hud.mode = MBProgressHUDModeIndeterminate
              hud.labelText = "Loading"
          }
      }
      

      如果你想关闭 HUD

      MBProgressHUD.hideHUDForView(self.view, animated: true)
      

      【讨论】:

        【解决方案6】:

        用 Swift 3 试试吧:

        func showLoadingHUD(to_view: UIView) {
            let hud = MBProgressHUD.showAdded(to: to_view, animated: true)
            hud.label.text = "Loading..."
        }
        
        func hideLoadingHUD(for_view: UIView) {
            MBProgressHUD.hideAllHUDs(for: for_view, animated: true)
        }
        

        如果 Swift 编译器警告:hideAllHUDs is deprecated. We should store references when using more than one HUD per view

        用途:

        func hideLoadingHUD(for_view: UIView) {
            MBProgressHUD.hide(for: for_view, animated: true)
        }
        

        【讨论】:

          【解决方案7】:

          使用以下代码呈现 MBProgressHUD,并在完成某些操作后将其隐藏,如所述。

          let spinnerActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true);
          
          spinnerActivity.label.text = "Loading";
          spinnerActivity.detailsLabel.text = "Please Wait!!";
          spinnerActivity.userInteractionEnabled = false;
          
          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0))
              {
                  // Add some background task like image download, wesite loading. 
          
                  dispatch_async(dispatch_get_main_queue())
                      {
                          spinnerActivity.hideAnimated(true);
                  }
          
          
              }
          

          有关更多信息,请遵循本教程: http://sourcefreeze.com/mbprogresshud-example-swift/

          【讨论】:

            【解决方案8】:

            添加到@EricDXS 的回答中,

            Swift 3 版本来了

            展示:

            let loadingNotification = MBProgressHUD.showAdded(to: self.view, animated: true)
            loadingNotification.mode = MBProgressHUDMode.indeterminate
            loadingNotification.label.text = "Loading"
            

            解雇:

            loadingNotification.hide(animated: true)
            

            【讨论】:

              【解决方案9】:

              我是这样解决的:

              import UIKit
              
              class Loader: NSObject {
              
                  class func show(message:String = "Processing...", delegate: UIViewController) {
                      var load : MBProgressHUD = MBProgressHUD()
                      load = MBProgressHUD.showHUDAddedTo(delegate.view, animated: true)
                      load.mode = MBProgressHUDMode.Indeterminate
              
                      if message.length > 0 {
                          load.labelText = message;
                      }
              
                      UIApplication.sharedApplication().networkActivityIndicatorVisible = true
              
                  }
              
              class func hide(delegate:UIViewController) {
                  MBProgressHUD.hideHUDForView(delegate.view, animated: true)
                      UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                  }
              }
              

              【讨论】:

                【解决方案10】:

                第 1 步:下载“MBProgressHUD.h”和“MBProgressHUD.m”文件并将这两个文件添加到您的项目中。它会要求您桥接目标 C 文件。如果您已经完成了桥接,则它不会询问。

                第 2 步:在桥接文件中导入 MBProgressHUD "import MBProgressHUD.h"

                第 3 步:使用以下代码显示进度界面或隐藏界面。

                展示

                DispatchQueue.main.async {
                    MBProgressHUD.showAdded(to: self.view, animated: true)
                }
                

                隐藏

                DispatchQueue.main.async {
                    MBProgressHUD.hide(for: self.view, animated: true)
                }
                

                【讨论】:

                • 非常没必要
                猜你喜欢
                • 2015-08-25
                • 2018-04-13
                • 2014-11-17
                • 2017-04-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多