使用 UIDocumentPickerViewController 在 Swift 中导入文本 [英] Using UIDocumentPickerViewController to import text in Swift

查看:46
本文介绍了使用 UIDocumentPickerViewController 在 Swift 中导入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently taking an iOS development course and as part of my project, I'm tasked with using UIDocumentPickerViewController to import text. Every example I've found is either a) written in Objective-C or b) is for importing UIImage files.

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

以下是我目前所获得的:

我设置了 iCloud 功能.作品

I have the iCloud capability set up. Works

指定委托,如下:

class MyViewController: UIViewController, UITextViewDelegate, UIDocumentPickerDelegate

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

I have the property set up for the type of text I'm trying to import:

@IBOutlet weak var newNoteBody: UITextView!

我有一个 IBAction 设置如下:

I have an IBAction setup as follows:

@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)

}

我不知道下面的线应该是什么.Apple 网站上的文档不清楚,我发现的每个示例都使用 Objective-C 或用于图像.

I can't figure out what the line should be below. The documentation on Apple's website isn't clear and every example I've found is in Objective-C or is for images.

// 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!)
    }
}

推荐答案

明白了!我有两个问题:

Got it! I had two problems:

1) Apple 说您必须在数组中指定 UTI.我将 documentType 称为 KUTTypeText.它应该是数组中的 "public.text".

1) Apple says you've gotta specify UTI's in an array. I called the documentType a KUTTypeText. It should be a "public.text" in the array.

这是 Apple 的 统一文本列表标识符 (UTI)

Here's Apple's listing of Uniform Text Identifiers (UTIs)

@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)

}

第二个问题是 Delegate 的语法问题,解决方法如下:

The second problem was a syntactic issue on the Delegate, solved with this:

// 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!)
    }
}

这篇关于使用 UIDocumentPickerViewController 在 Swift 中导入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆