0

I have two view controllers (autoClaimViewController and reviewAutoViewController). In autoClaimViewController, I have a dictionary (reviewTableViewData) that is made up of a struct called claimData (it contains two string variables and a UIImage variable). When the user hits the "Review" button, I want the reviewTableViewData dictionary to be passed to the second view controller so that it's data can be displayed on a table view in the second view controller (reviewAutoViewController). How do I pass this dictionary to the other view controller?

Please make your answers understandable for a beginner - I'm still learning. I'm moving between view controllers using storyboard segues.

Thanks.

Additional Question: Will the images that I stored in the variables be passed when the dictionary is passed? In other words, do images work like Integers and Strings, where they can be passed between variables without an issue?

My code:

struct claimData {
    var images = UIImage()
    var imageTitle = String()
    var relatedUnrelated = String()
}

class autoClaimViewController: UIViewController {

var reviewTableViewData = [claimData]()


   @IBAction func reviewButton(_ sender: Any) {

        //PASSES THE INFORMATION TO THE REVIEW VIEW CONTROLLER
        //FIX THIS... IT NEEDS TO SEND THE reviewTableViewData ARRAY.
        //MAKE A DICTIONARY IN THE REVIEW CONTROLLER THAT RECEIVES THE DICTIONARY FROM THIS VIEW CONTROLLER.

        let vc2 = reviewAutoViewController()
        //Find out how to transfer a dictionary from one view controller to another

    }
Christian W
  • 353
  • 2
  • 12
  • How are you moving from 1 view to the other? Using Segues you can pass the data in the segue. – Joby Ingram-Dodd Apr 10 '20 at 18:13
  • This is a fundamental skill of app development, and something that needs to be learned. You can either find an answer on StackOverflow (this has been answered many times before), or even better you can learn the ins and outs via a tutorial that covers all the ways you can do this. https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/ – Jeremy H Apr 10 '20 at 18:18
  • Does this answer your question? [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Jeremy H Apr 10 '20 at 18:19
  • I'm moving between the view controllers with segues. How would I declare the initial variable that I wanted to assign the dictionary to? – Christian W Apr 10 '20 at 21:00
  • You would just declare the same variable in both view controllers then you can pass backwards and forwards between them in the segues, my answer below should give you the basis to do this. – Joby Ingram-Dodd Apr 12 '20 at 07:12

1 Answers1

0

I think your best bet is to set up a segue in your storyboards which takes you from 1st view to the second view. Here is a guide.

https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html

In both views have a place to store the information you want to pass. So create a var in the secon view to hold reviewTableViewData from the first view.

Then in the 1st view you call

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.destination is reviewAutoViewController
    {
        let vc = segue.destination as? TertiaryViewController
        vc?.reviewTableViewData = reviewTableViewData
    }
}

This get the data ready to be sent. Then you perform the segue and the data should be passed for you. Perfomr the seugue with this func triggered by the button or whatever you use to tranisiton between views.

func performSegue(withIdentifier identifier: String, 
           sender: Any?){
    enter code here
}
Joby Ingram-Dodd
  • 730
  • 5
  • 23