使用MBProgressView

时间:2017-02-10 05:47:00

标签: swift selector mbprogresshud

我正在尝试使用带有MBProgressView的取消按钮。我收到错误"无法转换类型'()'的值期望参数类型'选择器'"

hud.button.addTarget(hud.progressObject, action: cancelButton(), for: .touchUpInside)

我也试过这样做:

hud.button.addTarget(hud.progressObject, action: #selector(cancelButton), for: .touchUpInside)

我收到了错误" #selector的参数无法引用本地函数' cancelButton()'"。

任何人都可以向我解释我做错了什么吗?

cancelButton应该在viewDidLoad中,或者至少我需要找到一种方法来访问viewDidload中的内容,因为我需要使用hud和snapshot.progress来取消下载:

   override func viewDidLoad() {
        super.viewDidLoad()

        let appdelegate = UIApplication.shared.delegate as! AppDelegate
        appdelegate.orintation = UIInterfaceOrientationMask.allButUpsideDown
        if book?.bookPath !=  book?.bookPath {
            print("HERE \(book?.bookPath)")
            loadReader(filePaht: (book?.bookPath)!)
        } else {
            let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let strName = book?.id
            let filePath = "\(documentsPath)/"+strName!+".pdf"
            let fileManager = FileManager.default

            if fileManager.fileExists(atPath: filePath) {
                loadReader(filePaht: filePath)
                return;
            }

            print("DOWNLOAD #1")

            let reference = FIRStorage.storage().reference(forURL: (self.book?.bookURL)!)
            let downloadTask =  reference.data(withMaxSize: 50 * 1024 * 1024) { (data, error) -> Void in
                if (error != nil) {

                } else {

                    if ((try! data?.write(to: URL.init(fileURLWithPath: filePath, isDirectory: false))) != nil) {
                        self.db.upDate(id: (self.book?.id)!, bookPath: filePath)
                        self.loadReader(filePaht: filePath)
                    }
                }
            }
            downloadTask.observe(.resume) { (snapshot) -> Void in
                // Download resumed, also fires when the download starts
            }
            downloadTask.observe(.pause) { (snapshot) -> Void in
                // Download paused
            }


                downloadTask.observe(.progress) { (snapshot) -> Void in

            DispatchQueue.global(qos: .default).async(execute: {() -> Void in
                self.showHUDWithCancel("Downloading")
                DispatchQueue.main.async(execute: {() -> Void in
                })
            })

            self.hud.progressObject = snapshot.progress

        }
            downloadTask.observe(.success) { (snapshot) -> Void in
                // Download completed successfully

                print("Download Success")

                SwiftLoader.hide()
            }

            downloadTask.observe(.failure) { (snapshot) -> Void in
                //Download failed

                print("Download failed")
            }


        }


    }
    func showHUDWithCancel(_ aMessage: String) {
        self.hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        self.hud.mode = MBProgressHUDMode.annularDeterminate
        self.hud.label.text = aMessage
        self.hud.detailsLabel.text = "Tap to cancel"
        let tap = UITapGestureRecognizer(target: self, action: #selector(cancelButton))
        self.hud.addGestureRecognizer(tap)
    }

    func cancelButton() {

       self.hud.hide(animated: true)
        self.hud.progressObject?.cancel()

        print("cancel button is working")

    }

这是取消按钮功能

 func cancelButton() {

        MBProgressHUD.hide(for: view, animated: true)
         snapshot.progress?.pause()

    }

picture 1 picture 2

3 个答案:

答案 0 :(得分:5)

试试这个 -

showHUDWithCancel以下的地方拨打你要添加hud的地方。

class ViewController: UIViewController {
   var hud = MBProgressHUD()

  override func viewDidLoad() {
    super.viewDidLoad()

  }
  func showHUDWithCancel(_ aMessage: String) {
    self.hud = MBProgressHUD.showAdded(to: self.view, animated: true)
    self.hud.label.text = aMessage
    self.hud.detailsLabel.text = "Tap to cancel"
    let tap = UITapGestureRecognizer(target: self, action: #selector(cancelButton))
    self.hud.addGestureRecognizer(tap)
}

func cancelButton() {
    self.hud.hide(animated: true)
    // do your other stuff here.
 }
}

viewDidLoad中添加此代码即可。

 override func viewDidLoad() {
    super.viewDidLoad()

    let appdelegate = UIApplication.shared.delegate as! AppDelegate
    appdelegate.orintation = UIInterfaceOrientationMask.allButUpsideDown
    if book?.bookPath !=  book?.bookPath {
        print("HERE \(book?.bookPath)")
        loadReader(filePaht: (book?.bookPath)!)
    } else {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let strName = book?.id
        let filePath = "\(documentsPath)/"+strName!+".pdf"
        let fileManager = FileManager.default

        if fileManager.fileExists(atPath: filePath) {
            loadReader(filePaht: filePath)
            return;
        }

        print("DOWNLOAD #1")

        let reference = FIRStorage.storage().reference(forURL: (self.book?.bookURL)!)
          downloadTask =  reference.data(withMaxSize: 50 * 1024 * 1024) { (data, error) -> Void in
            if (error != nil) {

            } else {

                if ((try! data?.write(to: URL.init(fileURLWithPath: filePath, isDirectory: false))) != nil) {
                    self.db.upDate(id: (self.book?.id)!, bookPath: filePath)
                    self.loadReader(filePaht: filePath)
                }
            }
        }
        downloadTask.observe(.resume) { (snapshot) -> Void in
            // Download resumed, also fires when the download starts
        }
        downloadTask.observe(.pause) { (snapshot) -> Void in
            // Download paused
        }

       downloadTask.observe(.progress) { (snapshot) -> Void in OperationQueue.main.addOperation {
            OperationQueue.main.addOperation {
                self.hud.progressObject = snapshot.progress
                self.showHUDWithCancel("Downloading")
            }
          }
        }


        downloadTask.observe(.success) { (snapshot) -> Void in OperationQueue.main.addOperation {
            // Download completed successfully

            print("Download Success")
                OperationQueue.main.addOperation {
                 SwiftLoader.hide()
                }
            }
        }

        downloadTask.observe(.failure) { (snapshot) -> Void in OperationQueue.main.addOperation {
            //Download failed

            print("Download failed")
               OperationQueue.main.addOperation {
            _ = self.navigationController?.popViewController(animated: false)
            }
        }
        }


    }


}

答案 1 :(得分:0)

downloadTask方法范围之外的viewDidLoad定义移入类本身。这样您就可以直接访问任务,而不是通过观察者传递的snapshot或者附加到downloadTask或progressHUD的progress。这样,您可以从视图控制器的任何方法访问任务,包括cancelButton()

task.pause()

而不是

snapshot.progress?.pause()

最终代码可能如下:

class ViewController: UIViewController {

    var downloadTask: FIRStorageDownloadTask!

    ...

    override func viewDidLoad() {
        super.viewDidLoad()

        ...

        let reference = FIRStorage.storage().reference(forURL: (self.book?.bookURL)!)
        downloadTask = reference...

        ...

    }

}

答案 2 :(得分:0)

注意:对于那些使用最新版本的MBProgressView的用户,按钮文档已更改:

/**
 * A button that is placed below the labels. Visible only if a target / action is added and a title is assigned..
 */

因此,创建的内容应类似于以下内容:

class Tools {

  static func popLoadingDialog(viewParent: UIView,
                                 label: String,
                                 cancelTarget: Any? = nil,
                                 cancelSelector: Selector? = nil) -> MBProgressHUD {

        let loadingNotification = MBProgressHUD.showAdded(to: viewParent, animated: true)
        loadingNotification.mode = MBProgressHUDMode.indeterminate
        loadingNotification.label.text = label
        if(cancelSelector != nil) {
            loadingNotification.button.setTitle("Cancel", for: .normal)
            loadingNotification.button.addTarget(cancelTarget, action: cancelSelector!, for: .touchUpInside)
        }
        return loadingNotification
    }
}

并调用它:

loadingIndicator = Tools.createLoadingDialog(viewParent: view,
                                                      label: "Please wait...",
                                                      cancelTarget: self,
                                                      cancelSelector: #selector(onCancelClick))
loadingIndicator?.show(animated: true)

}

@objc func onCancelClick(){
        // do something when the user click on cancel...
}