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.
Asked
Active
Viewed 510 times
-4
-
What have you tried? Do you have code that you have tried and didn't work as you expected? If so include that in your question. – MwcsMac Jun 19 '18 at 16:15
-
No I don't really know where to start so I don't have anything yet. – Emmett Miller Jun 19 '18 at 17:55
2 Answers
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
-
Thank you! I used UserDefaults actually and it worked quite well. Appreciate it! – Emmett Miller Jun 19 '18 at 20:15