-1

I'm trying to pass a basic string array between two ViewControllers, each of which controls a different tab view in an iOS Xcode project. I collect the data from a JSON file in one of the controllers, and I want to pass various string arrays I'll collect from that data to a different tab, or the other ViewController. How would I go about doing this?

I'm fairly new to Swift and Xcode, so apologies if this is a stupid question. Any help is appreciated!

  • User singleton class design pattern for this purpose or you may use protocol too for data communication between controllers – Muhammad Awais Mar 02 '20 at 06:20
  • 1
    Possible duplicate of: [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – TylerP Mar 02 '20 at 07:01

1 Answers1

0

You can use the prepare(for segue: UIStoryboardSegue, sender: Any?) function if you are using storyboards:

Simply override this function in your first UIViewConteroller (i.e. the instance that holds the data that you want to pass to the second UIViewController):

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if var viewController = segue.destination as? MySecondViewController
    {
        // Set data
        // viewController.stringArray = self.stringArray
    }
}
inexcitus
  • 2,471
  • 2
  • 26
  • 41
  • I've tried implementing this, but it's not working- does it still work with tab views? I have two scenes, each of which is controlled by a ViewController. All of the examples I've seen of this function isn't with a tab view. – Spencer Michaels Mar 02 '20 at 07:29