如何用swift使用MBProgressHUD

这里是我的代码,但它显示了进展。 这个代码有没有错误? 请给一些想法来解决这个问题,或者给一些相关的链接。

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

已更新答案:

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

解散ProgressHUD:

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

您也可以尝试这种方法,使其他活动在后台运行,使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... } } 

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 

通过下面的代码

 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) 

使用下面的代码来呈现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/

加上@ EricDXS的回答,

Swift 3版本在这里

以显示:

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

解雇:

  loadingNotification.hide(animated: true) 

我解决了这个问题:

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

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

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

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

第3步:使用下面的代码来显示进度hud或隐藏hud。

为展示

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

为了隐藏

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