Matthew
Matthew

Reputation: 47

(Swift) Save data from UITextField into Firebase Firestore

Currenty the code shown saves the String "TEST" in the Firestore database. What I need to have happen is for the text entered into the UITextField to be saved when the buttons pressed and that data be saved in the Firestore database.

For my IBAction func, I believe that it is correct but I do not know how to take the train variable and place it in my dict without getting an error.

import Foundation
import UIKit
import Firebase
import FirebaseUI

class TrainViewController: UIViewController{

    @IBOutlet weak var trainField: UITextField!
    @IBOutlet weak var locationField: UITextField!
    @IBOutlet weak var engineField: UITextField!

    @IBAction func saveButton(_ sender: UIButton) {
        var train: String = trainField.text!

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        trainField.delegate = self
        var dict = [String:String]()
        dict.updateValue("TEST", forKey: "train")

        let db = Firestore.firestore()
        db.collection("users").addDocument(data: dict)
    }
}

extension TrainViewController : UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    } 
}

Upvotes: 0

Views: 1447

Answers (2)

Jay
Jay

Reputation: 35658

You may want to collect the string from the field in the action and then pass it to a function for saving.

class TrainViewController: UIViewController{

    @IBOutlet weak var trainField: UITextField!
    @IBOutlet weak var locationField: UITextField!
    @IBOutlet weak var engineField: UITextField!

    @IBAction func saveButton(_ sender: UIButton) {
        let trainText = trainField.text!
        self.saveText(theText: trainText)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

   func saveText(theText: String) {
       let db = Firestore.firestore()
       let dict = ["train_text": theText]
       db.collection("users").addDocument(data: dict)
   }
}

Will result in the following being written to Firestore

your_firestore
   users
     random_doc_id
        train_text: "the passed in string"

EDIT

Based on some new info, the OP would like to take the text from three text fields and write them all to a single document.

So change the button action thusly - all it does is calls our saveText function and does not pass any data.

   @IBAction func saveButton(_ sender: UIButton) {
        self.saveText()
    }

then the saveText function

   func saveText() {
       let trainText = self.trainField.text!
       let locationText = self.locationField.text!
       let engineText = self.engineField.text!

       let db = Firestore.firestore()
       let dict = ["train_text": trainText,
                   "location_text": locationText,
                   "engine_text": engineText]
       db.collection("users").addDocument(data: dict)
   }

and the resulting Firestore looks like this

your_firestore
   users
     random_doc_id
        train_text: "text from the train field"
        location_text: "text from the location field"
        engine_text: "text from the engine field"

Upvotes: 1

CoderMan
CoderMan

Reputation: 182

This is fairly simple to achieve, what you need to do is declare a function that saves the data after it's entered.

The reason you aren't able to save the trainField is because when the ViewController calls viewDidLoad the button hasn't been pressed, therefor you aren't getting the text from the trainTextField.

To solve this you can do the following:

fileprivate func saveTrainWithData(text: String) {
    let dictData = ["train": text] as [String:Any]
    // save data to FireStore here using `dictData`
    let db = Firestore.firestore()
    db.collection("users").addDocument(data: dictData)
}

Now to fire this function off just call it inside of the @IBAction as shown below

@IBAction func saveButton(_ sender: UIButton) {
    var train: String = trainField.text!
    saveTrainWithData(text: train)
}

Upvotes: 0

Related Questions