Permanent Data and Prepare For Segue Permanent Data and Prepare For Segue xcode xcode

Permanent Data and Prepare For Segue


Ok so because you said you want to persist the data over app starts you will need the User Defaults to store your items. As Dan already suggested you are basically doing it right. You just want to set a variable that was not declared before. But I will show you this in the following code. I will also attach a second approach in which the items are passed to next view controller while performing the segue.

First example: Imagine we have two view controllers like in your example. The first view controller contains a UITextField to do user text input. Whenever we switch from the first view controller to the second view controller with the help of a storyboard segue (e.g. when pressing a button) we take the existing texts from previous segues from the User Defaults and add the current user input and then persist it back to the User Defaults. This happens in the first view controller:

class ViewController: UIViewController {@IBOutlet weak var textField: UITextField!override func prepare(for segue: UIStoryboardSegue, sender: Any?) {    super.prepare(for: segue, sender: sender)    if segue.identifier == "toSecondViewController" {        let itemsFromDefaults = UserDefaults.standard.object(forKey: "items")        var items: [String]        if let tempItems = itemsFromDefaults as? [String]        {            items = tempItems            items.append(textField.text!)        }        else        {            items = [textField.text!]        }        print(items)        UserDefaults.standard.set(items, forKey: "items")    }}}

This first view controller looks pretty similar to your code but I wanted to add it for completeness.

Then in the second view controller we just grab the items from the user defaults and store it directly in a instance variable of this view controller. With the help of this we can do what we want in other methods within the view controller and process the items further. As I said what you were missing was the instance variable declaration to store the items in.

class ViewController2: UIViewController {private var items: [String]? // This is only accessible privatelyoverride func viewDidAppear(_ animated: Bool) {    super.viewDidAppear(animated)    self.items = UserDefaults.standard.object(forKey: "items") as? [String]}}

Second Example: You could also declare a internal/public variable in ViewController2 so that you can set it directly from the first view controller in perform segue. Than you wouldn´t need to grab the items from the User Defaults in ViewController2. For that you can access the destination view controller of the segue then cast it to ViewController2 and directly set the items of it.

class ViewController: UIViewController {@IBOutlet weak var textField: UITextField!override func prepare(for segue: UIStoryboardSegue, sender: Any?) {    super.prepare(for: segue, sender: sender)    if segue.identifier == "toSecondViewController" {        let itemsFromDefaults = UserDefaults.standard.object(forKey: "items")        var items: [String]        // [...] Do the stuff to get the items and add current input like before [...]        let destinationViewController = segue.destination as! ViewController2        destinationViewController.items = items    }}}class ViewController2: UIViewController {var items: [String]? // This is accessible from outside nowoverride func viewDidAppear(_ animated: Bool) {    super.viewDidAppear(animated)    print(items) // We can now print this because it is set in prepareForSegue}}

I really hope I could help you with that and explained it understandable. Feel free to leave a comment if you have any questions.


You forget to write "let" or "var" in viewDidAppear. Try this:

override func viewDidAppear(_ animated: Bool) {    let itemsObject = UserDefaults.standard.object(forKey: "items")    if let tempItems = itemsObject as? [String] {        let items = tempItems    }}

If you want to use items after if statement, then you must declare variable before if statement:

override func viewDidAppear(_ animated: Bool) {    let itemsObject = UserDefaults.standard.object(forKey: "items")    var items: [String]    if let tempItems = itemsObject as? [String] {        items = tempItems    }}