2017-02-23 37 views
-1

我試圖把一個documentPicker從iCloud獲取文件,但實際上我的應用程序打開documentMenu導入文件(iCloud,Dropbox),當我選擇iCloud時,我提出了文件選擇器與我的文件。當我委派文件documentPicker(_ ...)[documentPicker.delegate = self.delegate]功能不會被調用,因爲我的課不符合協議DocumentPickerDelegate無法工作後更新到SWIFT 3

import UIKit 

class ImportKeyViewController: UIViewController{ 
    @IBOutlet weak var openWithLabel: UILabel! 
    @IBOutlet weak var transparentBackgroundView: UIView! 
    @IBOutlet weak var iCloudButton: UIButton! 
    @IBOutlet weak var iTunesButton: UIButton! 

    weak var delegate : UIDocumentPickerDelegate? 

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL){ 
     print("Entre a documentPicker") 
    } 

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { 
     print("Sali a documentPicker") 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     print("llegue") 
     setUpUI() 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 

     UIView.animate(withDuration: 0.1, animations: { 
      self.transparentBackgroundView.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 0.7) 
     }) 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     UIView.animate(withDuration: 0.1, animations: { 
      self.transparentBackgroundView.backgroundColor = UIColor.clear 
     }) 
    } 

    @IBAction func closeButtonAction(_ sender: UIButton) { 

     let documentPicker = UIDocumentPickerViewController (documentTypes: ["public.text","public.content"], in: .import) 
     documentPicker.delegate? = self.delegate! 
     self.present(documentPicker, animated:true, completion: nil) 

    } 
} 

我有強制性和UIDocumentPickerDelegate的可選方法,但它不工作,因爲你可以在下面的圖片看到

這是我的第一個iOS應用程序,希望你能幫助我。

+0

沒有 「你可以在圖像的下方看到」 有。 – Gruntcakes

+0

請通過複製和粘貼相關代碼來對您的問題進行編輯,並清楚說明哪些部分有效,哪些部分沒有。 – rmaddy

+0

謝謝。我的錯。圖片已更新 –

回答

0

您應該添加didPickDocumentAt:委託方法的新語法,看起來好像有點改變。

- 編輯

ImportKeyViewController應該繼承UIDocumentPickerDelegate。一個很好的方法是創建一個擴展,這樣就很容易察覺委託方法是什麼方法,以及你的方法是什麼。

像這樣:

extension ImportKeyViewController: UIDocumentPickerDelegate { 

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL){ 
     print("Entre a documentPicker") 
    } 

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { 
     print("Sali a documentPicker") 
    } 

} 
+0

這一個? func documentPicker(_ controller:UIDocumentPickerViewController,didPickDocumentAt url:URL){...} –

+0

在您發佈的圖片中,現在已經移除了,您可能會注意到編譯器錯誤。關於您的視圖控制器沒有向委託人確認。您可以通過點擊命令並單擊UIDocumentPickerDelegate來檢出委託方法。 –

+0

現在我明白你做錯了什麼,讓我編輯我的答案。 –

相關問題