Call struct from different view controller (swift4)

I am trying to cal the let name statement inside struct person from twoVIEWCONTROLLER. I thought I can just call a structs entries from another view controller but I am not sure. You can see in twoVIEWCONTROLLER i have “l.text =”. I tried l.text = person.name but that does not work.

VIEWCONTROLLER

         import UIKit
          class ViewController: UIViewController {
        @IBOutlet var a: UITextField!
          @IBOutlet var label: UILabel!
var contacts = [Person]()

@IBAction func save(_ sender: Any) {
    contacts.append(Person(name: a.text!))
    contacts.sort { $0.name < $1.name }

}}

    struct Person: CustomStringConvertible {
let name: String

var description: String {
    return name
}}

twoVIEWCONTROLLER

            import UIKit

 class twoViewController: UIViewController {
   @IBOutlet var l: UILabel!
  

override func viewDidLoad() {
    super.viewDidLoad()

    l.text = 
}}

It isn’t complete solution, but it will give you an idea

class twoViewController: UIViewController {
    @IBOutlet var l: UILabel!
    
    //TODO: get referanceToOtherViewController with some logic
    //TODO: comment this line and uncomment the next one
    var referanceToOtherViewController: ViewController? = ViewController()
//    weak var referanceToOtherViewController: ViewController?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addAPersonOnOtherVCWhenUsingPlaygroundInsteadOfBuildingVCs()
        
        let text = referanceToOtherViewController?.contacts.first?.name
        print(text) // Jhon Silver
        l.text = referanceToOtherViewController?.contacts.first?.name
    }
    
    // using this instead of running in Simulator
    func addAPersonOnOtherVCWhenUsingPlaygroundInsteadOfBuildingVCs() {
        let newPerson = Person(name: "Jhon Silver")
        referanceToOtherViewController?.contacts.append(newPerson)
    }
    
}


Thanks for the response @ofiron! Anyway the code is not transferring the value from vc to vc2. When I applied your code “Jhon Silver” is always on vc2 but whatever I enter into vc is not being transferred.

Hi @timswift, I’m afraid I didn’t explain myself as I should have.
Let me try again.

I assume the following:

  1. You use storyboard
  2. In ViewController we enter the the data to the contact variable
  3. We use segue to move to TwoViewController after pressing on a button

My solution is (with swift pseudo code):

  • in ViewController we use the prepareforsegue function to assign the reference
    • I will use option 2 or 3, it least recommend to use option 1 (if you want I can explain)
  • in TwoViewController use the reference
//in ViewController

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "moveToTwoVC", let twoVC = segue.destination as? TwoViewController {
             // option 1
            twoVC.referanceToOtherViewController = self
            
            // option 2
            twoVC.contacts = contacts
            
            // option 3
            twoVC.person = contacts.first
        }
    }
// in TwoViewController

// option 1
weak var referanceToOtherViewController: ViewController?

// option 2 (can be ? or !)
var contacts: [Person]?

// option 3 (can be ? or !)
var person: Person?

override func viewDidLoad() {
    super.viewDidLoad()
    // option 1
    l.text = referanceToOtherViewController.contacts.first.name
   // option 2
   l.text = contacts.first.name
  // option 3
  l.text = person.name
}

Just a quick point. In the original struct the name part was set to “let”, if it’s going to change should it not be “var”?

One way to look at var vs let, is to set everything to let, and when you need to change the variable, just change the let to var.

Thanks ofiron, I agree that the solution you proposed is absolutely the correct way to pass a variable between 2 view controllers (and to suggest anything else, is not in any way beneficial). You however can use a struct to pass variables between different classes (useful if using mvc architecture), but the way it was written meant that person.name was constantly seen as “”, and because the variable is wrapped in a struct Xcode struggles to pick up this up. Sorry for the confusion, and thank you for the reply.

This topic was automatically closed after 166 days. New replies are no longer allowed.