在 Swift 中使用 segue 传递 Firestore 的 documentID

Passing documentID of Firestore using segue in Swift

我正在通过制作应用来学习 Swift 和 firebase。在我当前的 firestore 中,我有一个包含集合名称 Players 的数据库,并且在集合内部,有一个包含自动文档 ID 和两个字段的文档,例如名称和电子邮件。像这样。 firestore image

我也使用 Firebase Auth,所以每当用户创建帐户时,我都会将他们的电子邮件放入字段名称为 email 的 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)给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)
       }
   }   
}

//超出registerPressed 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
   }
 }