我正在通过制作应用程序来学习Swift和Firebase。在当前的Firestore中,我有一个数据库,该数据库的集合名称为Players,并且在集合内部,有一个文档带有auto-documentID和两个字段,例如name和email。像这样。
firestore image
我还使用Firebase Auth,因此无论何时用户注册帐户,我都将其电子邮件与字段名称电子邮件一起放入Firestore。
这是RegisterViewController的代码,其中用户使用电子邮件和密码注册帐户。

import UIKit
import Firebase

class RegisterViewController: UIViewController {


    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!

    let db = Firestore.firestore()

    override func viewDidLoad() {
        super.viewDidLoad()

    }



    //MARK: - register a new account

    @IBAction func registerPressed(_ sender: UIButton) {

        if let email = emailTextField.text, let password = passwordTextField.text {

            Auth.auth().createUser(withEmail: email, password: password) { authResult, error in


                if error != nil {

                // some codes here


                } else {

                    // Add a new document with a generated ID
                    var ref: DocumentReference? = nil

                    ref = self.db.collection(K.FStore.playersCollection).addDocument(data: [K.FStore.emailField: email, K.FStore.nameField: ""]) { (error) in

                        if let err = error {
                            print("There was an issue storing data to firestore. \(err)")
                        } else {
                            print("Document added with ID: \(ref!.documentID)")
                        }
                    }

                    // pass this data

                    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                        if segue.identifier == K.registerToHome {
                            let homeVC = segue.destination as! HomeViewController

                            homeVC.documentID = ref?.documentID
                        }
                    }

                     self.performSegue(withIdentifier: K.registerToHome, sender: self)

                }

            }

        }
    }



}
我打算检查用户在下一个HomeViewController的文档名称字段中是否有名称,因此如果文档中名称字段的值是"",我想显示一个带有textfeild的警报,以便用户可以添加他们的名字。但是,在HomeViewController中,我需要documentID,因为我将在HomeVC中使用此代码db.collection(K.FStore.playersCollection).document(documentID).getDocument,所以我想传递用户的documentID(在RegisterVC中,我能够将值作为ref?.documentID,所以我只想传递将值设为HomeVC)。我尝试使用func prepare(for segue: UIStoryboardSegue, sender: Any?),但是无法传递值...

最佳答案

将您的代码更改为:
//您的代码顶部定义id:

var id = ""

else {
   // Add a new document with a generated ID
   var ref: DocumentReference? = nil

   ref = self.db.collection(K.FStore.playersCollection).addDocument(data: [K.FStore.emailField: email, K.FStore.nameField: ""]) { (error) in

       if let err = error {
          print("There was an issue storing data to firestore. \(err)")
       } else {
          print("Document added with ID: \(ref!.documentID)")
          self.id = ref!.documentID
          self.performSegue(withIdentifier: K.registerToHome, sender: self)
       }
   }
}
//已取消注册Pressed func:
// pass this data

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if segue.identifier == K.registerToHome {
        let homeVC = segue.destination as! HomeViewController

         homeVC.documentID = self.id
   }
 }

关于ios - 在Swift中使用segue传递Firestore的documentID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63452055/

10-11 20:26