簡體   English   中英

在UITextView Swift 3中保存文本

[英]Saving text in UITextView Swift 3

我正在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()

}

}    

知道如何顯示嗎?
創建的名為Note Entity Attribute detailsString類型

在iOS應用程序中處理數據


如果您想將數據保存在應用程序內部,則需要在應用程序內部做更多的事情來保存數據。 這樣,您可以將數據保存在應用程序天氣終止應用程序內部,它將顯示您保存的數據並再次獲取。

1.)對於短日期保存,可以使用UserDefaults

2.)通過使用SQLite

3.)通過使用Coredata

4.)通過使用Realm ,有關更多詳細信息,請參見“ 示例”。

您需要創建數據庫以根據需要每次保存文本值。

您可以使用以下任一方法創建數據庫:

  1. Core data 從這里獲取教程

  2. SQLite 從這里獲取教程

使用以下任何一種保存文本數據,然后獲取數據並在UI上分配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM