2017-02-10 42 views
0

我想使用MBProgressView取消按钮。我收到错误“无法转换类型的值‘()’到期望的参数类型‘选择’”取消按钮使用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

+0

凡已添加'cancelButton '功能? –

+0

@NiravD这就是我需要访问的问题let hud = MBProgressHUD.showAdded(to:self.view,animated:true)来隐藏它。它在viewdidload中,但我不能在viewdidload之外的cancelButton函数和访问hud – Khallad

+0

你可以从viewDidLoad发布代码吗? –

回答

3

试试这个 -

请从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中即可使用。的downloadTaskviewDidLoad方法范围到类本身以外

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


    } 


} 
+0

我试过这个,但它根本没有显示 – Khallad

+0

这是测试代码和它正在为我工​​作..好吧告诉我它不可见或取消不工作? –

+0

它是不可见 – Khallad

0

移动定义。这样,您就可以直接访问任务,而不是通过在观察者中传递的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... 

     ... 

    } 

} 
+0

如果我移动downloadTask在viewDidload之外,我将不得不移动let reference = FIRStorage.storage()。reference(forURL:(self.book ?. bookURL)!),并且由于book.bookURL的原因,我不得不这么做。那怎么可能呢? – Khallad

+0

@tom,答案更新 –

+0

我现在要试试 – Khallad