user7054537
user7054537

Reputation:

Permanent Data with Prepare For Segue

I am currently trying to use Permanent Data and Prepare For Segue together. This is what I have so far: (VC1)

override func viewDidAppear(_ animated: Bool) {

    let itemsObject = UserDefaults.standard.object(forKey: "items")


    if let tempItems = itemsObject as? [String] {

        items = tempItems

}

(VC2):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toSecondViewController" {

        let itemsObject = UserDefaults.standard.object(forKey: "items")

        var items:[String]

        if let tempItems = itemsObject as? [String] {

            items = tempItems

            items.append(textField.text!)

            print(items)

        } else {

            items = [textField.text!]

        }

        UserDefaults.standard.set(items, forKey: "items")


    }
}

I am trying to add an item to an array on VC2. Then I want to transfer the array to VC1 whilst storing it permanently. I am using Xcode 8.0 and Swift 3.0.

Upvotes: 0

Views: 171

Answers (2)

Joe
Joe

Reputation: 8986

Your question only explains.how to pass and store data permanently. But, you haven't mentioned about where you want to save data. I presume,you using tableView to store data and retrieving it.

In AppDelegate applicationDidFinishLaunching register an empty array as default value for the key "NewArray".

  let defaultValues = ["NewArray": [String]()]
  UserDefaults.standard.register(defaults: defaultValues)

In Second VC define newArray (variable names are supposed to start with a lowercase letter) also as empty String array

 var newArray = [String]()

In viewDidLoad retrieve the array from user defaults, append the new item, save the array back to user defaults and reload the table view

 override func viewDidLoad() {
 super.viewDidLoad()

 let defaults = UserDefaults.standard
 newArray = defaults.array(forKey: "NewArray") as! [String]
 newArray.append(newItem) //newItem is the data.Which you passing from First VC
 defaults.set(newArray, forKey: "NewArray")
 self.tableView?.reloadData()
}

and you don't need to use segue on your second VC unless you passing data from it...

Upvotes: 1

Josh Homann
Josh Homann

Reputation: 16327

Its the other way around; when your source view controller initiates a segue from either a storyboard or performSegue, the source view controller's prepareForSegue method gets invoked. With in that method you should determine the type of view controller you are segueing to and set the properties accordingly. Note that you don't really need the identifier unless you have more than one segue to the same destination in which case its sometimes useful to know which one is being invoked.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let notificationsViewController = segue.destination as? NotificationsViewController {
            NotificationsViewController.notification = notification
        }
    }

Upvotes: 1

Related Questions