-4

I am currently programming an app with two storyboards. One storyboard is an onboarding storyboard where I obtain crucial info from the user in order to run the app. Another is the main app. How can I create variable that is able to be manipulated and accessible to both storyboards? I am using Swift.

2 Answers2

0

Accordingly to this post here in Stackoverflow

var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! MyCustomViewController

controller.exercisedPassed = "Ex1"

self.presentViewController(controller, animated: true, completion: nil)
GuiDupas
  • 1,681
  • 2
  • 22
  • 44
0

I tend to go back and fourth between using UserDefaults and sending data via prepareForSegue

UserDefaults Guide : Setting And Getting Data With UserDefaults In Swift

prepareForSegue sample : stackoverflow prepareForSegue sample

With prepareForSegue, create empty variables in your destination ViewController that will receive data, then set them in the prepareForSegue function in your origin view controller.

Example

class DestinationViewController: UIViewController{
      var value1: String!
      var value2: Int!
}


//SWIFT 4
class OriginViewContoller: UIViewContoller{
      override func prepare(for segue: UIStoryboardSegue, sender: Any?){
          switch(segue.identifier){
             case "SEGUE_IDENTIFIER_HERE":
                 let vc = segue.destination as! DestinationViewController
                 vc.value1 = "Some String"
                 vc.value2 = 1234
          }
      }
}

If I am using UserDefaults to keep track of important user data, I make sure to remove this data from UserDefaults when the user logs out

Example

UserDefaults.standard.removeObject(forKey: "SOME_KEY_IDENTIFIER_HERE")
Roshan
  • 110
  • 2
  • 10