【发布时间】: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