2017-02-10 44 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