在将数据从一个ViewController传递到下一个ViewControl时遇到问题



我几个月来学得很快,有一件事我一直搞砸了,那就是在ViewControllers之间传递数据。当我从prepareForSegue方法中PO前瞻性变量的路径时,该值是完整的。但当新的ViewController实际出现时,我检查了它的值,此时它为零。如果有人能为我指明正确的方向,我将不胜感激。

class LoginViewController: UIViewController {
var user_ID:String = ""
//this below is within another method activated by button
Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
if error != nil {
print ("there was an error signing in")
print (error!.localizedDescription)
return
}
else {
//go to home screen
let userUID = result?.user.uid
print (userUID)
self.user_ID = userUID

self.performSegue(withIdentifier: "MainSegue", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

//this is the destination VC
let viewController = segue.destination as! ViewController

viewController.user_ID = self.user_ID

//NOTE: if I break here and PO viewConroller.user_ID the value is intact
let homeViewController = (self.storyboard?.instantiateViewController(withIdentifier: "MainVC"))! as! ViewController

//let mainViewController = ViewController()
//mainViewController.user_ID = userUID

self.present(homeViewController, animated: true, completion: nil)

}




class ViewController: UIViewController {
var persons = [Person]()
let db = Firestore.firestore()
var user_ID:String = ""
//NOTE: WHEN `viewdidload` runs value of user_ID is nil

Phillip在上面的评论中提供了解决方案。当前的ViewController出现问题。赛格已经做到了。非常感谢。

最新更新