0

I'm making a basic app that allows user login.

I wanted to know the general practice of passing user data between view controllers. After a user is authenticated, is it generally acceptable to pass their data through view controllers?

For example, user A is logged in and authenticated. User A wants to "create a event", to be able to save that the user is hosting this event, I would have to store the users ID on the event as host and I would need access to his UID. Would you save the users ID after login and keep passing it in to every view?

I am using Firebase as my backend if that helps.

jshah
  • 1,599
  • 2
  • 17
  • 38

2 Answers2

2

You should learn about a thing called "segues". It is the primary mechanism to transition between views in ios.

Here's a good tutorial(no personal affiliation, btw) http://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/

haven't used firebase, but if you are passing event data from scratch by yourself you would do 4 things.

  1. would declare the var in the target controller.
  2. in the source controller you would fire off the function: performSegueWithIdentifier at the appropriate time.
  3. that takes you to your prepareForSegue function (the contents of this func is like the last point of no return before you transition to the new controller). inside, create a var where the name of the type is the name of the target view controller.
  4. use dot operators to access the variable you want to pass to and give it the appropriate value
stanley
  • 1,113
  • 1
  • 12
  • 26
0

Yes, best practice is to pass data between your view controllers as needed. You usually hook into the prepareForSegue method to accomplish that. Don't fall into the all-too-common practice of using (abusing, rather) your app delegate to maintain a bunch of global state. That is bad news. It is cleaner and more modular, in my view, to pass data down the view controller hierarchy, rather than having your view controllers reach back "up" and grabbing what they need.

Reid
  • 1,109
  • 1
  • 12
  • 27