1

Swift question - I want to pass the values of an instance of a struct from one view controller to another. In the first view controller I created an instance of the struct, assigned values to the various properties, and now, on the click of a button, want to be taken to the second view controller and have the struct from the first view controller at my disposal.

This is what I have in the first view controller below, but not part of, the buttonPressed action.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destVC = segue.destination as! HoleTrackerViewController
    destVC.myRound = newRound
}

myRound is the instance of the struct I created in the second view controller. newRound is the instance from the first view controller with the values assigned to the properties.

When I'm taken to the new view controller, the myRound instance doesn't have any of the values that the newRound instance had.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
  • How did you created the segue in Storyboard? From the `UIButton` to `HoleTrackerViewController` or from FirstViewCoontroller to `HoleTrackerViewController`? I think you did the first one. Remove it, and redo it as the second option? – Larme May 26 '23 at 17:20

1 Answers1

0

Based on your code snippet, it seems like you're on the right track to pass the values of the struct instance from one view controller to another. However, there might be an issue with the timing or initialization of the newRound instance.

Make sure you are assigning the values to the newRound instance before performing the segue. You can do this in the buttonPressed action before triggering the segue.

Here's an updated example to clarify the steps:

In your first view controller:

struct MyStruct {
    var property1: String
    var property2: Int
    // Add other properties as needed
}

class FirstViewController: UIViewController {
    var newRound: MyStruct!
    
    @IBAction func buttonPressed(_ sender: UIButton) {
        // Assign values to newRound before performing the segue
        newRound = MyStruct(property1: "Value 1", property2: 42)
        
        performSegue(withIdentifier: "YourSegueIdentifier", sender: self)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "YourSegueIdentifier" {
            if let destinationVC = segue.destination as? SecondViewController {
                destinationVC.myRound = newRound
            }
        }
    }
}

In your second view controller:

class SecondViewController: UIViewController {
    var myRound: MyStruct!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Access the values of myRound
        print(myRound.property1)
        print(myRound.property2)
        // Access other properties as needed
    }
}

Ensure that you have properly set the segue identifier in your storyboard and that the destination view controller is of the correct type (SecondViewController in this example).

By assigning values to the newRound instance before performing the segue, you should be able to access those values in the second view controller through the myRound property.

HangarRash
  • 7,314
  • 5
  • 5
  • 32