【发布时间】: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 称为 Note 与 Attribute details 类型 String
【问题讨论】:
-
那么您面临的问题是什么?
-
不知道怎么弄
-
你需要选择某种形式的持久化机制; Core Data,一个文本文件,一个 plist。并保存您的数据并在您的应用启动时加载它
-
我尝试使用 Core Data 但无法显示它们