Swift 在将数据从一个ViewController传递到下一个ViewController时遇到问题
几个月来我一直在学习swift,有一件事我一直搞砸了,那就是在ViewController之间传递数据。当我从Swift 在将数据从一个ViewController传递到下一个ViewController时遇到问题,swift,viewcontroller,Swift,Viewcontroller,几个月来我一直在学习swift,有一件事我一直搞砸了,那就是在ViewController之间传递数据。当我从prepareForSegue方法中输入前瞻变量的路径时,该值是完整的。但当新的ViewController实际出现时,我检查了它的值,此时它为零。如果有人能给我指出正确的方向,我将非常感激 class LoginViewController: UIViewController { var user_ID:String = "" //this below is w
prepareForSegue
方法中输入前瞻变量的路径时,该值是完整的。但当新的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就是问题所在。赛格已经做到了。谢谢你 self.user_ID,您在哪里初始化用户_ID?我已在LoginViewController(原始VC)和ViewController类(目标)中声明:var user_ID:String=“”,不创建和显示第二个视图控制器。让segue在
目的地中放置一个。您正在一个实例中设置数据,然后呈现另一个实例。谢谢Phillip!!!你说得很对,这很有效。你是个天才!!!