web-dev-qa-db-fra.com

Comment utiliser MBProgressHUD avec swift

voici mon code, mais il montre les progrès. y a-t-il une erreur dans ce code? donnez s'il vous plaît une idée pour résoudre ceci, ou donnez un lien lié à ceci.

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

}
39
vimal prakash

Réponse mise à jour:

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

Pour rejeter ProgressHUD:

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

Vous pouvez également essayer cette approche qui gardera l’autre activité en arrière-plan, permettant ainsi à l’UI de rester réactive, offrant ainsi une meilleure expérience utilisateur. C'est l'approche envisagée/recommandée pour utiliser 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...
    }
}
13
The Crazy Chimp

Swift 3 extensions

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
9
Chris Van Buskirk

J'ai ajouté une extension sur UIViewController pour faciliter les choses. 

Swift 4.1

import UIKit
import MBProgressHUD

extension UIViewController { 

    func showHUD(progressLabel:String){
        let progressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
        progressHUD.label.text = progressLabel
    }

   func dismissHUD(isAnimated:Bool) {
        MBProgressHUD.hide(for: self.view, animated: isAnimated)
    }
}

Usage:

 //Add hud for showing progress
 self.showHUD(progressLabel: "Sending...")

 //Hide Hud
  DispatchQueue.main.async {
     self.dismissHUD(isAnimated: true)
  }

J'espère que ça aide!!!

5
Harjot Singh

Passer par le code ci-dessous

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

Si vous voulez ignorer le HUD

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

Ajouté à la réponse de @ EricDXS,

La version de Swift 3 est ici

Montrer:

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

De rejeter:

loadingNotification.hide(animated: true)
2
Janmenjaya

Essayez-le avec 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)
}

Si avertissement du compilateur Swift: hideAllHUDs is deprecated. We should store references when using more than one HUD per view

Utilisation:

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

Utilisez le code ci-dessous pour rendre MBProgressHUD et, après une action terminée, masquez-le comme décrit. 

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


    }

Pour plus d’informations, suivez ce tutoriel: http://sourcefreeze.com/mbprogresshud-example-Swift/

2
Bharathi Devarasu

Je l'ai résolu comme ça:

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
    }
}
1
vimal prakash

étape 1: Téléchargez les fichiers "MBProgressHUD.h" et "MBProgressHUD.m" et ajoutez-les à votre projet. il vous demandera de créer un pont pour les fichiers Objective C. si vous avez déjà fait le pontage, il ne vous le demandera pas.

étape 2: Dans le fichier de pontage, importez MBProgressHUD "import MBProgressHUD.h"

étape 3: utilisez le code ci-dessous pour afficher les progrès hud ou masquer hud.

pour le spectacle

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

pour cacher

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