2017-02-10 12 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()'을 참조 할 수 없습니다.

누구든지 나에게 잘못을 설명 할 수 있습니까?

취소 버튼이있는 viewDidLoad에 있어야 또는 내가 다운로드 취소 HUD와 snapshot.progress를 사용할 필요가 있기 때문에 적어도 내가있는 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)!) 
      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

'함수? –

+0

@NiravD 내가 액세스해야하는 문제는 hud = MBProgressHUD.showAdded (to : self.view, animated : true)로 숨겨야합니다. viewdidload에 있지만 viewdidload 외부에있는 cancelButton 함수를 사용할 수없고 동시에 hud에 액세스 할 수 없습니다. – Khallad

+0

viewDidLoad에서 코드를 게시 할 수 있습니까? –

답변

3

이 시도 -

Cancel을 사용하여 hud를 추가하려는 곳에서 showHUDWithCancel으로 전화하십시오.

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 안에이 코드를 추가하십시오. 클래스 자체에있어서 viewDidLoad 범위 이외의 downloadTask

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() 포함하여 뷰 컨트롤러의 방법에서 작업에 액세스 할 수 있습니다 이렇게 : 대신

snapshot.progress?.pause() 

최종 코드의

task.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 이동하면 viewDisload 외부에서 reference = FIRStorage.storage(). reference (forURL : (self.book?.bookURL)!)를 움직여야 할 것입니다. book.RL 때문에 book_RL을 사용합니다. 어떻게 그렇게 할 수 있니? – Khallad

+0

@tom, 대답 업데이트 –

+0

지금 당장 시험해 보겠습니다. – Khallad