使用UIDocumentPickerViewController在Swift中导入文本

阿德里安

我目前正在上iOS开发课程,并且作为我项目的一部分,我的任务是使用UIDocumentPickerViewController导入文本。我发现的每个示例都是a)用Objective-C编写的,或者b)是用于导入UIImage文件的。

如何设置文本文件的委托方法?

到目前为止,这是我得到的:

我已经设置了iCloud功能。作品

指定委托,如下所示:

class MyViewController: UIViewController, UITextViewDelegate, UIDocumentPickerDelegate

我为要导入的文本类型设置了属性:

@IBOutlet weak var newNoteBody: UITextView!

我有IBAction如下设置:

@IBAction func importItem(sender: UIBarButtonItem) {

    var documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: [kUTTypeText as NSString], inMode: UIDocumentPickerMode.Import)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
    self.presentViewController(documentPicker, animated: true, completion: nil)

}

我不知道下面该行。苹果网站上的文档不清楚,我发现的每个示例都在Objective-C中或用于图像。

// MARK: - UIDocumentPickerDelegate Methods

func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
    if controller.documentPickerMode == UIDocumentPickerMode.Import {
        // What should be the line below?
        self.newNoteBody.text = UITextView(contentsOfFile: url.path!)
    }
}
阿德里安

得到它了!我有两个问题:

1)苹果表示您必须在数组中指定UTI。我称documentType为a KUTTypeText它应该"public.text"在数组中。

这是苹果公司的统一文本标识符(UTI)列表

@IBAction func importItem(sender: UIBarButtonItem) {

    var documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.text"], inMode: UIDocumentPickerMode.Import)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
    self.presentViewController(documentPicker, animated: true, completion: nil)

}

第二个问题是代表身上的句法问题,此问题已解决:

// MARK: - UIDocumentPickerDelegate Methods

func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
    if controller.documentPickerMode == UIDocumentPickerMode.Import {
        // This is what it should be
        self.newNoteBody.text = String(contentsOfFile: url.path!)
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Swift在SpriteKit中导入外部节点

使用Cocoapods在Swift中导入框架的问题

在Swift中导入Swift框架:“使用未声明的类型'MyCustomView'”

使用 VBA 在 excel 中导入特定文本的过程

在Swift框架中导入CommonCrypto

在Swift中导入如何工作?

使用Swift 3.0编译的Realm的Swift模块不能在Swift 3.0.1中导入

使用Swift 4.0编译的Realm的Swift模块不能在Swift 4.0.2中导入

在swift中导入swift链接的objc类

在Elasticsearch中导入文本文件

在 Access 中导入文本文件?

Python:从输入包中导入“文本”

使用Swift 4.0编译的模块无法在Swift 4.0.1中导入

使用Swift 4.0编译的模块无法在Swift 3.2.2中导入:AudioKit

使用Swift 3.0编译的模块无法在Swift 2.3中导入

使用Swift 4.1.2编译的模块无法在Swift 4.1.50中导入:Xcode 10错误

使用Swift 4.0编译的模块无法在Swift 3.2.1中导入

使用swift X.1编译的模块不能在Swift X.0.2中导入

使用swift 3.0编译的模块不能在Swift 3.0.1中导入

使用swift 4.0编译的模块无法在swift 3.1中导入

使用Swift 4.0.3编译的AudioKit模块无法在Swift 4.1中导入

使用Swift 4.1编译的模块无法在Swift 4.1.50中导入

如何使用.split()从较大的gzip文件中导入制表符分隔的文本?大块?

使用 np.loadtxt 从格式奇怪的文本文件中导入数据

如何使用UIDocumentPickerViewController一次导入多个文件?

如何在Swift的JavaScriptCore中导入模块?

在 Swift 中导入 Objective-c 框架?

在Swift中导入C标头

如何在XCTest for Swift中导入模型