2021年2月6日星期六

UserDefault value for UITextField is the same for all items in a TableView

I have a UITableView with a bunch of different items from an array that I get from JSON using a URL endpoint.

For each object, I can tap on the cell to see more details (I've passed the details for each item for the details screen with no problem). On this details screen, there is also a text field where I can add a note.

I'm trying to use UserDefaults to save the text field text for when i leave the screen or exit the app entirely. The value is still there for each cell when I go back, but the problem is that the text field entry is the same for every item in the table view and I want each one to have a unique note.

Here is my code from the DetailViewController:

This is a function that gets called when a button is pressed that will save the data and go back to the table view controller:

@objc func saveNote() {      let noteDefault = UserDefaults.standard      noteDefault.setValue(noteTextField.text, forKey: "Note")      navigationController?.popViewController(animated: true)  }  

And I have this function in viewDidLoad:

func saveUserDefault() {  let noteDefault = UserDefaults.standard  if (noteDefault.value(forKey: "Note") != nil) {  noteTextField.text = noteDefault.value(forKey: "Note") as? String      }  }  
https://stackoverflow.com/questions/66083891/userdefault-value-for-uitextfield-is-the-same-for-all-items-in-a-tableview February 07, 2021 at 10:02AM

没有评论:

发表评论