Как использовать 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)
}
}
}
Ответы
Ответ 1
Обновленный ответ:
let loadingNotification = MBProgressHUD.showAdded(to: view, animated: true)
loadingNotification.mode = MBProgressHUDMode.indeterminate
loadingNotification.label.text = "Loading"
Чтобы отклонить ProgressHUD:
MBProgressHUD.hideAllHUDs(for: view, animated: true)
Ответ 2
Вы также можете попробовать этот подход, который будет поддерживать другую активность в фоновом режиме, позволяя пользовательскому интерфейсу оставаться отзывчивым, предоставляя пользователям лучший опыт. Это рекомендуемый/рекомендуемый подход для использования 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
Быстрые 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
Пройдите приведенный ниже код
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)
Ответ 5
Используйте приведенный ниже код для рендеринга 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/
Ответ 6
Добавлен ответ @EricDXS,
версия Swift 3 здесь
Чтобы показать:
let loadingNotification = MBProgressHUD.showAdded(to: self.view, animated: true)
loadingNotification.mode = MBProgressHUDMode.indeterminate
loadingNotification.label.text = "Loading"
Отклонить:
loadingNotification.hide(animated: true)
Ответ 7
Я решил это так:
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
}
}
Ответ 8
Попробуйте с помощью 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)
}
Ответ 9
Шаг 1: Загрузите файл "MBProgressHUD.h" и "MBProgressHUD.m" и добавьте оба файла в свой проект. он попросит вас создать мосты для файлов Objective C. если вы уже сделали перемычку, тогда он не спросит.
Шаг 2: В Bridging File import MBProgressHUD "import MBProgressHUD.h"
Шаг 3: используйте код ниже, чтобы показать прогресс hud или скрыть hud.
для Show
DispatchQueue.main.async {
MBProgressHUD.showAdded(to: self.view, animated: true)
}
для скрытия
DispatchQueue.main.async {
MBProgressHUD.hide(for: self.view, animated: true)
}