【问题标题】:Saving text in UITextView Swift 3在 UITextView Swift 3 中保存文本
【发布时间】:2017-01-28 09:12:40
【问题描述】:

我正在 IOS 平台 Swift 3 上创建一个待办事项应用程序 我正在尝试在 UITextView 中保存注释,因此当我回击或终止应用程序时,数据将被保存。

StoryBoard 在导航栏上有一个 UITextView 和一个保存按钮 如何让用户在 UITextView 中输入文本并保存

class Details: UIViewController, UITextViewDelegate{

// MARK: - IB
@IBOutlet weak var noteText: UITextView!
@IBAction func addNote(_ sender: UIButton) {

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
   let addNote = Note(context: context)
    addNote.details = noteText.text!

    //Saving
    (UIApplication.shared.delegate as! AppDelegate).saveContext()

}

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var Notes: [Note] = []

func getData() {
    do {
        Notes = try context.fetch(Note.fetchRequest())
    } catch {
        print("Fetching Failed")
    }
}

override func viewWillAppear(_ animated: Bool) {
    getData()
}


override func viewDidLoad() {
    super.viewDidLoad()
    let MyIcon = UIImageView(image: UIImage(named: "037_Pen"))
    self.navigationItem.titleView = MyIcon
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}    

知道如何显示它吗?
创建 Entity 称为 NoteAttribute details 类型 String

【问题讨论】:

  • 那么您面临的问题是什么?
  • 不知道怎么弄
  • 你需要选择某种形式的持久化机制; Core Data,一个文本文件,一个 plist。并保存您的数据并在您的应用启动时加载它
  • 我尝试使用 Core Data 但无法显示它们

标签: ios swift


【解决方案1】:

在 iOS 应用程序中处理数据


如果您想将数据保存在应用程序中,那么您需要在应用程序中执行更多操作以保存数据。这样,您可以在应用程序天气终止应用程序中保存数据,它将显示您保存的数据并再次获取。

1.) 对于短日期保存可以使用 UserDefaults

2.) 通过使用SQLite

3.) 通过使用Coredata

4.) 使用Realm,更多详情请查看Example.

【讨论】:

  • 是的,也可以使用Realm,但基本级别就足够了。 @takethefake
  • 听起来像 CordData、SQLite、Realm 是多余的——也许只是写入文件?
  • 不像它以正确的方式保存您的数据并保存图像和文档等文件,我们使用的是DocumentDirectory.@Ashley Mills
【解决方案2】:

您需要创建数据库以根据您的要求每次保存文本值。

您可以使用以下任一方法创建数据库:

  1. Core dataGet tutorial from here

  2. SQLiteGet tutorial from here

使用任何人保存您的文本数据,然后获取数据并在 UI 上分配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    相关资源
    最近更新 更多