Using variables in multiple ViewControllers

This is planned to be a multi tab app. There’d be a home screen with some information about the app and a button that has to be pressed to move further in the process. Pressing the button, random numbers should be generated and stored in an array. On the second screen the randomized numbers should be displayed in labels.

The things I did: I set up the multi tab look in the main storyboard. The first on is controlled by FirstViewController, the second screen is controlled by the SecondViewController. Yay, so far so good.
Also, I created a new file (rangomizer.swift) to handle all the randomization process.

The first problem I faced was that only the FirstViewController understood the variables and functions of the randomizer.swift, the SecondViewController doesn’t know what to do with any of them. (I don’t really understand why, if you could clarify it to me would be great.) Anyway, in the FirstViewController I managed to code that if you press the button, the randomizing function is executed and also, the randomized integers are converted to strings so the labels would be able to use them. But this is where I’m absolutely stuck, because I’m not able to tell the FirstViewController to modify the label.text in the SecondViewController, nor tell the SecondViewController to use variables from the FirstViewController.

Could you suggest me a solution?

You need to either pass the array from one view controller to another. (Google passing data between view controllers)

OR you could save the array into a UserDefault

1 Like