简体   繁体   中英

Saving text in UITextView Swift 3

I am creating a To-Do App on IOS Platform Swift 3 I am trying to save note in UITextView so when i hit back or terminate application the data is saved.

StoryBoard Have a UITextView and a save button at the navigation bar How to make user enter his text in UITextView and save it

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

}

}    

Any idea how to display it ?
Created Entity called Note with Attribute details of type String

Dealing with data in iOS application


If you want to save your data inside application then you need to do something more inside your application for data saving purpose. This way you can save data inside application weather terminate application it will show your saved data and fetch again.

1.) For Short Date save can use UserDefaults

2.) By using SQLite

3.) By Using Coredata

4.) By Using Realm , For more details check Example.

You need to create database to save the text value every time as per your requirement.

You can create the database by using any one of the below :

  1. Core data Get tutorial from here

  2. SQLite Get tutorial from here

Save your text data by using anyone these and then fetch the data and assign at the UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM