首页 > 解决方案 > UIKit 中的 Xcode 文档选择器委托

问题描述

为什么我不能将委托设置为自我?我在 viewcontroller.swift 文件中运行下面的代码,但我想在 Render.swift 文件中调用它。我有一个运行相机视图的金属场景,想打开它前面的文件选择器,但这似乎不是很简单。

@IBAction func importFiles(_ sender: Any) {
    
    let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePlainText as String], in: .import)
    documentPicker.delegate = self
    documentPicker.allowsMultipleSelection = false
    present(documentPicker, animated: true, completion: nil)
}

它说无法将“ViewController”类型的值分配给“UIDocumentPickerDelegate”类型

在我的 Renders.swift 文件中,我有一个绑定到按钮的函数,该按钮调用以下内容:

 //open dialog for picker...
    let myVC: ViewController = ViewController()
    
    myVC.importFiles()

我对这一切都很陌生。

标签: iosxcodeviewcontrolleribaction

解决方案


为什么我不能将委托设置为自我?

因为委托需要是 UIDocumentPickerDelegate,而不是selfUIDocumentPickerDelegate 。您需要声明它是:

class ViewController : UIViewController, UIDocumentPickerDelegate {

这可能会引发其他问题(我希望它会),但至少你会超越现在的状态。


推荐阅读