0
votes

What is the best way to add an item to an array on one VC and then use "prepare for segue" to transfer the array to another VC? So far this is what I have managed to come up with: (VC1)

var items: [String] = ["Hello"]

(VC2):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    var destViewController: ViewController = segue.destination as! ViewController
    destViewController.items = [textField.text!]
    items.append(textField.text!)
}

On VC2 an error is coming up that states, "use of unresolved identifier" on the line

items.append(textField.text!)
3
use it directly destViewController.items.append(textField.text!) - zombie
Would I need to remove destViewController.items = [textField.text!] and replace it with destViewController.items.append(textField.text! - D. Murphy
both two last lines - zombie
when you said items.append it will look into the properties of VC2 and since it won't find anything it wouldn't know how to handle. This is the definition of Object Oriented Programming. Thought the beauty is if you have created another object, you can call on itself its properties so destViewController.items.apped(textField.text!) would work for you. - mfaani

3 Answers

5
votes

I am pretty new to iOS/Swift but I recently ran into the same situation. Here is how I do it.

SourceViewController.swift

class SourceViewController: UIViewController {
    let stringToPass = "Hello World"

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as! DestinationViewController
        destinationVC.receivedString = stringToPass
        }
    }

DestinationViewController.swift

class DestinationViewController: UIViewController {

    var receivedString: String?

    if let newString = receivedString {
        print(newString)
    }
...

I realize this is slightly different than your example, but the important thing to note is that when you create "destinationVC" you are then able to modify the properties of it. The key difference is you have to provide the scope of the variable (destinationVC.receivedString) when assigning a value or in your case appending to an array:

destViewController.items.append(textField.text!)

Without providing the scope Xcode is unable to find the variable (identifier) you are trying to modify since it wasn't part of the current file or part of an import.

0
votes

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("thank you very much for your helpful suggestions")
print("I think I am pointed at the right direction")
print("Thanks again for your generous contributions")
print("Apoligies! Stackoverflow is pretty well automated to allow me to put it in non programatic format")
}
0
votes

I use this code for my projects, in case you use a UITableView:

// On first viewController
public override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue" {
        let vc = segue.destination as! VC2
        vc.data = sender as! [String]

    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    var dataArray: [String] = []

    dataArray.append(items[indexPath.row].id)
    dataArray.append(items[indexPath.row].title)
    dataArray.append( items[indexPath.row].details)

    performSegue(withIdentifier: "segue", sender: dataArray)


}

Just get on second your array

var data: [String] = []

id.text = data[0]
title.text = data[1]
details.text = data[2]