【问题标题】:Sending data from one view controller to another not working将数据从一个视图控制器发送到另一个不起作用
【发布时间】:2017-08-24 06:48:03
【问题描述】:

我正在尝试将一些数据从一个视图控制器发送到另一个视图控制器。但我似乎无法让这段代码工作!它应该将标签更改为传递给它的变量,但不会更改任何内容。

ViewController.swift

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "SVCSegue" {
        let controller = segue.destination as! SecondViewController
        if let user = FIRAuth.auth()?.currentUser {
            controller.email = user.email
        }
    }
}

SecondViewController.swift

@IBOutlet weak var usernameLabel: UILabel!

    var email:String!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.usernameLabel.text = self.email

    }

编辑我在override func prepare之后也有这个代码

@IBAction func loginAction(_ sender: Any) {

        if self.emailField.text == "" || self.passwordField.text == "" {
            let alertController = UIAlertController(title: "Oops!", message: "Please put in an email and password.", preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)

            alertController.addAction(defaultAction)

            self.present(alertController, animated: true, completion: nil)
        } else {
            FIRAuth.auth()?.signIn(withEmail: self.emailField.text!, password: self.passwordField.text!, completion: { (user, error) in
                if error == nil {
                    self.emailField.text = ""
                    self.passwordField.text = ""
                } else {
                    let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .alert)
                    let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)

                    alertController.addAction(defaultAction)
                }
            })
        }
    }

【问题讨论】:

  • 检查你的 segue 标识符是 SVCSegue 或其他东西。此外,在 else 块中传递默认电子邮件可能是您的 if let 条件未得到执行
  • prepare(for:) 中设置断点并逐步查看发生了什么。
  • 你是如何触发 segue 的?
  • 带有按钮。查看我的编辑。
  • 您不能直接从按钮使用动作; segue 将在 IBAction 和登录操作之前触发。登录成功后需要使用performSegue触发segue

标签: ios xcode firebase swift3 firebase-authentication


【解决方案1】:

我不确定,但是根据在序列期间调用 SecondViewController 的不同生命周期函数的时间,可能在执行准备代码之前调用了 viewDidLoad。

尝试为电子邮件设置一个默认值,以便您可以使用该值。另外,在 prepare 方法的 else 部分中将 email 值设置为其他值,以查看是否是 if 条件失败。

【讨论】:

  • 是的,它的 if let user 条件
【解决方案2】:
@IBAction func loginAction(_ sender: Any) {

        if self.emailField.text == "" || self.passwordField.text == "" {
            let alertController = UIAlertController(title: "Oops!", message: "Please put in an email and password.", preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)

            alertController.addAction(defaultAction)

            self.present(alertController, animated: true, completion: nil)
        } else {
            FIRAuth.auth()?.signIn(withEmail: self.emailField.text!, password: self.passwordField.text!, completion: { (user, error) in
                if error == nil {
               let sb = UIStoryboard(name: "StoryBoardName", bundle: nil)
    let secVC = sb.instantiateViewController(withIdentifier: "ViewControllerIdentifier") as! SecondViewController
                 if let userValue = FIRAuth.auth()?.currentUser {
                        secVC.email = userValue.email!
                    }
       self.navigationController?.pushViewController(secVC, animated: true)
                } else {
                    let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .alert)
                    let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)

                    alertController.addAction(defaultAction)
                }
            })
        }
    }

I have made change in loginAction() that is navigate to SecondViewController and pass data of user

Try above code , Hope this help you and solved your issue.

【讨论】:

  • 不。不工作。我应该删除override func prepare 函数吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多