0

I have a sign in page and after I sign in, i want my program to pass a variable to a bunch of view controllers that are connected by SWRevealViewController. But I don't know how to.

This is the code I use to pass the variable "username" to the first page of SWRevealViewController points to

let next = self.storyboard?.instantiateViewControllerWithIdentifier("reveal") as! SWRevealViewController
next.loadView()
let main = next.frontViewController as! mainPage
main.username = username as String
self.presentViewController(next, animated: true, completion: nil)

I passed it through front view controller because mainPage is identified with sw_front. But I want to pass this variable to all the view controllers under the SWRevealViewController I have.

Additionally, the way I passed this variable does work but because I called "next.loadView()" first, the variable would not be assigned value in viewLoad of mainPage. Anybody has any idea how to solve this too? I was thinking one of the possibilities might be passing data to rearviewcontroller first and pass it around but I don't know how to

Percy Teng
  • 53
  • 2
  • 9
  • 1
    Not heard of a Model? – Gruntcakes Jul 08 '16 at 15:05
  • Sorry, what's that? – Percy Teng Jul 08 '16 at 15:14
  • A Model-View-Controller (MVC) is a design pattern, its the most common and prevalent and well known one there is. Your question is crying out for you to implement a model to store and share your data between the view controllers. – Gruntcakes Jul 08 '16 at 15:20
  • so you mean I create a class containing a static variable to store the value of username when I sign in. After I sign in, I can just get the value from that model since it's static right? – Percy Teng Jul 08 '16 at 15:25
  • At its most basic level a model could be like that yes. You indicated below you've found an answer, good. But that doesn't mean you still shouldn't consider having a model, if you add more and more view controllers and they all need this data, and other data, then you can see why having a model is useful. A model does more than just store the data usually, it also provides functionality to get or store the data etc. Read up them even if you decide you don't want one now, you will need one in the future. – Gruntcakes Jul 08 '16 at 15:33
  • Ya I totally agree with u, I posted that answer cuz that basically solved my question but I prefer ur answer. U can add an answer and Ima try that later. If that works out I'll take ur answer – Percy Teng Jul 08 '16 at 15:55

1 Answers1

0

I figured it out by just passing data to my rear view controller and pass it through prepareSegue() to each sub view controller.

Percy Teng
  • 53
  • 2
  • 9