1

I am currently working on an app and have problems with posting events.

I collect data in a sequence of several views (7 screens) and would like to store the data finally at once (or show all the data in the final view). These data are, for example, location info, user info, event image, comments, event category, ... I know how to store the data in the database/storage (firebase) if I collect the data in one or two views. But in my use case I have seven views and I could not find any elegant method.

What's the best way to do that with Xcode 10?

user13489738
  • 37
  • 1
  • 5
  • 1
    It's marked as a duplicate already people, no need to downvote it. And you can see this guy left the community. People need to chill the f-out. – ScottyBlades Apr 28 '19 at 20:14

2 Answers2

8

You can use struct as below code. Make all required variable for all screen in this struct (like string, image etc..). And you can access this from any ViewController.

struct InputDetails {
    static var details: InputDetails = InputDetails()

    var city: String = ""
    var lat: String = ""
    var long: String = ""
}

Now to add value in this

InputDetails.details.city = textfiels.text

Now to access first screen value in last screen

print(InputDetails.details.city)

And once your API call or above struct usage is over, make sure to reset all details like below.

InputDetails.details = InputDetails()
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
  • This is a really simple and cool solution, I used it and it works well. I know it's probably a beginner question but I do want to ask one thing: how exactly does the static var work? What does it do? – CristianMoisei Jun 13 '19 at 23:13
  • 1
    we create one struct (InputDetails) and hold it's value in one static var (var details) so, once we store value in static variable it will hold it's value until we initialise that again (InputDetails()) – Hardik Thakkar Jun 14 '19 at 04:12
1

There are several ways for passing data between View Controllers. For example you could use an instance property or a segue or the delegation method.

I recommend you study this article which paints a complete picture of the different methods and how to apply them:

How To: Pass Data Between View Controllers In Swift

Edit:

Upon examining the picture in your question I figured that using a segue would be the most appropriate solution here. As it seems from the picture you enter data in one View Controller, pass that onto the second View Controller and finally you upload all the data to Firebase.

I assume that you use storyboards (if not then consult the link above for other methods.) In this example below you will pass a string from one VC to another.

Step 1:

Add a segue between two view controllers. Storyboard -> press ctrl and click on VC one and drag your mouse -> you will see a blue arrow, drag that to VC two and release -> select manual segue: show -> click on the segue and give it an identifier

Step 2:

In VC two, make a string variable:

class SecondViewController: UIViewController {
var stringToPass: String = ""

    override func viewDidLoad() {
    super.viewDidLoad()

    print(stringToPass)
}

Step 3: In VC one, enter the following:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? SecondViewController {
vc.stringToPass = "This is the string we pass between two VC"
     }
}

Step 4: Then whenever you want to go to the SecondViewController perform the segue like this:

performSegue(withIdentifier: "identifierYouEntered", sender: self)
lajosdeme
  • 2,189
  • 1
  • 11
  • 20
  • Would you mind enlightening me why the downvote? What do I got wrong in your opinion? – lajosdeme Feb 04 '19 at 17:56
  • thank you very much :-) :-) :-) a wonderful answer – user13489738 Feb 04 '19 at 18:49
  • Relating downvote, that’s not me. Sorry, I can’t give you additional points because I have less than 15 reputations :-) – user13489738 Feb 05 '19 at 18:31
  • 1
    I have no idea why anyone would downvote; this is a fine answer so upvote from me. But.. The OP wasn't asking about moving between the views so the segue info is not needed - seems he has that part down. Instead of the String, create a PersonClass object and pass it through the viewContollers, filling in properties as you go, then writing the PersonObject properties out to Firebase. The other option is a viewCoordinator where a master view creates each view as needed and 'holds' the PersonClass data. As each view is completed the data is passed back to the coordinator to populate the class. – Jay Feb 05 '19 at 21:05
  • 1
    oh and here's a more in depth explanation of that [Pass Model between view controllers](https://stackoverflow.com/questions/43815549/ios-how-to-pass-a-model-from-view-model-to-view-model-using-mvvm) – Jay Feb 05 '19 at 21:06
  • JohnLasta I'm happy that my answer did help you! :) @Jay thank you for the upvote! :) yes rereading the question I realize that a large part of it was about how to collect data across multiple views. Your comment does a perfect job answering that. – lajosdeme Feb 05 '19 at 21:55