Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 3将数据从一个ViewController传递到另一个ViewController_Ios_Swift - Fatal编程技术网

Ios Swift 3将数据从一个ViewController传递到另一个ViewController

Ios Swift 3将数据从一个ViewController传递到另一个ViewController,ios,swift,Ios,Swift,我对IOS编程还不熟悉,不知道我在问什么,但我会尽力解释 我正在使用Firebase进行身份验证,然后我想获取UID并将其传递给另一个VC我遇到的问题是我无法获得var userID打印出iAction这就是我目前的位置,任何指针都会得到很好的支持 @IBAction func creatAccountPressed(_ sender: Any) { if let email = emailTextField.text, let password = passwordTe

我对IOS编程还不熟悉,不知道我在问什么,但我会尽力解释

我正在使用
Firebase
进行身份验证,然后我想获取UID并将其传递给另一个VC我遇到的问题是我无法获得
var userID
打印出
iAction
这就是我目前的位置,任何指针都会得到很好的支持

@IBAction func creatAccountPressed(_ sender: Any) {
    if let email = emailTextField.text,
        let password = passwordTextField.text,
        let name = nameTextField.text {
        Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
            if let firebaseError = error {
                print(firebaseError.localizedDescription)
            }
            let userID            = user!.uid
            let userEmail: String = self.emailTextField.text!
            let fullName: String  = self.nameTextField.text!
            self.ref.child("users").child(userID).setValue(["Email": userEmail, "Name": fullName])
            self.userID1          = user!.uid as! String
        })
        print(self.userID1)
        presentLoggedInScreen()
    }
}

func presentLoggedInScreen() {
    let stroyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let loggedInVC: LoggedInVCViewController = stroyboard.instantiateViewController(withIdentifier: "loggedInVC") as! LoggedInVCViewController
    self.present(loggedInVC, animated: true, completion: nil)
}

在完成区块内。目前,这些线路几乎肯定会在完成块之前发生,因为该块是在所需的网络调用返回后执行的

请确保将
presentLoggedInScreen()
包装在一个块中,该块在主线程接触UI时将其分派给主线程,并且所有UI调用都必须从主线程进行

DispatchQueue.main.async {
  presentLoggedInScreen()
}
这将使
presentLoggedInScreen()
在分配给
userID1
的值之后执行,从而允许您将值传递给传入的视图控制器(当然,假设传入的VC有一个适当的变量来传递
userID1

比如:

loggedInVC.userID = self.userID1

在展示VC之前。

一种更简单的方法是通过initilizer或在展示第二个VC之前设置的变量将信息从一个VC传递到另一个VC

由于您是新手,现在请尝试变量方法,因此如果您正在传递字符串:

class LoggedInVCViewController : UIViewController {

    var info : String? {
       didSet {
          if let newInfo = self.info {
             //do what ever you need to do here
          }
       }
    }

    override viewDidLoad() {
       super.viewDidLoad()

    }

}

func presentLoggedInScreen(yourInfo: String) { 
   let stroyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
   let loggedInVC:LoggedInVCViewController = 
   storyboard.instantiateViewController(withIdentifier: "loggedInVC") as! 
   LoggedInVCViewController 
   loggedInVC.info = yourInfo
   self.present(loggedInVC, animated: true, completion: nil) 
}
请注意,对于
info
,您始终可以使用任何其他变量类。您还可以通过编程让VC在属性的get括号内执行一些方法,根据信息内容填充一些字段,加载特定的UI等

另一个有用的方法是进行初始化,但不幸的是,您不能将其用于故事板NIB(我知道这很遗憾,但这很好地解决了这一问题),但只要您能够轻松地以编程方式初始化和设计VC,它仍然是有用的(如果您是VC,我会尽快学习)

在VC的自定义初始化器方法中传递变量:

class LoggedInVCViewController : UIViewController {

    var info : Any? {
        get {
            if let this = self.info {
                return this
            } else {
                return nil
            }
        } set {
            if let new = newValue {
                //
            }
        }
    }

    init(info: Any?) {
        //This first line is key, it also calls viewDidLoad() internally, so don't re-write viewDidLoad() here!!
        super.init(nibName: nil, bundle: nil)

        if let newInfo = info {
            //here we check info for whatever you pass to it
            self.info = newInfo
        }
    }

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

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
因此,您将使用is作为:

func presentLoggedInScreen(yourInfo: String) {
        let loggedInVC = LoggedInVCViewController(info: yourInfo)
        self.present(loggedInVC, animated: true, completion: nil)
}
显然,如上所述,这种方法不能用于故事板,但非常有用,而且,正如我相信您可以看到的,它更紧凑

我建议你熟悉这些文档,以及一些博客提示,如。你可以在一段时间后变得很有创意

为了学习更多的编程方法,我强烈推荐YouTube频道:,我个人还没有找到更好的编程方法Swift编程参考点

不要犹豫问问题

更新

您的IBAction应该如下所示:

@IBAction func creatAccountPressed(_ sender: Any) {
    if let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text {
        Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
            if let firebaseError = error {
                print(firebaseError.localizedDescription)
                }


            let userID = user!.uid
            let userEmail: String = self.emailTextField.text!
            let fullName: String = self.nameTextField.text!

            self.ref.child("users").child(userID).setValue(["Email": userEmail, "Name": fullName])

           self.userID1 = user!.uid as! String

           print(self.userID1)

           presentLoggedInScreen(yourInfo: self.userID1)


        })

    }
}

显示函数presentLoggedInScreen()funct presentLoggedInScreen(){让stroyboard:UIStoryboard=UIStoryboard(名称:“Main”,bundle:nil)让loggedInVC:LoggedInVCViewController=stroyboard.InstanceeviewController(标识符为:“loggedInVC”)作为!LoggedInVCViewController self.present(loggedInVC,动画:true,完成:nil)}@PaulSmith不在评论中发布代码。请将您的问题与所有相关细节联系起来。谢谢您的回复,我将查看youtube视频。您在顶部的方法是有效的,但我的问题是userID1在iAction中获得了它的值,所以当我尝试打印它或使用它执行任何操作时,它都不起作用,我说的对吗在iAction中有一个var在该操作之外没有办法使用它?有一种方法,就像另一个replyer一样,你只需要将print()和present方法放在completion块中,但是,就像我的答案告诉你的那样,你需要从presentLoggedInScreen内部访问该变量。我的方法是(将userID作为参数传递),或者从方法内部访问它,应该可以工作。
@IBAction func creatAccountPressed(_ sender: Any) {
    if let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text {
        Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
            if let firebaseError = error {
                print(firebaseError.localizedDescription)
                }


            let userID = user!.uid
            let userEmail: String = self.emailTextField.text!
            let fullName: String = self.nameTextField.text!

            self.ref.child("users").child(userID).setValue(["Email": userEmail, "Name": fullName])

           self.userID1 = user!.uid as! String

           print(self.userID1)

           presentLoggedInScreen(yourInfo: self.userID1)


        })

    }
}