TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

day 74 milestone project covering projects 19-21. Saving and Loading UITextView.

Forums > 100 Days of Swift

I am having loads of trouble saving each note individually. I used a simple array to hold the notes in ViewController class and retrieved the same array inside DetailViewController as you will see in my code. So far, I've gotten to notes to save but all 4 notes are saving the same text when I want them to be save individually and I think my error is coming from theDidSelectRowAt() method in my viewController Class but I am not totally sure. I think my issue is that I am not saving the textView of each individual note. I don't know how to do that.

here's my ViewController


import UIKit

class ViewController: UITableViewController {
    var notes = ["Note1", "Note2", "Note3", "Note4"]
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return notes.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        cell.textLabel?.text = notes[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController{
            vc.selectedNote = notes[indexPath.row]

          navigationController?.pushViewController(vc, animated: true)
        }

    }

    }

and here's my DetailViewController


import UIKit

class DetailViewController: UIViewController {
    @IBOutlet var textView: UITextView!
    var selectedNote: String?
    var notesTwo = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        let otherVC = ViewController()

            notesTwo = otherVC.notes
            //retrieving the text user entered from userDefaults
            let defaults = UserDefaults.standard
            if let noteText = defaults.string(forKey: "noteText"){

                textView.text = noteText
            }

        textView.font = UIFont(name: "Helvetica-Bold", size: 18)

    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

            //saving the text user enters using userDefaults
            if let noteText = textView.text{
                let defaults = UserDefaults.standard
                defaults.set(noteText, forKey: "noteText")
            }
    }

}

3      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.