0

I'm not sure why, but the segue is triggered even when the email & password field are empty. How can it possibly be?

import UIKit
import Firebase

class RegisterViewController: UIViewController {

@IBOutlet weak var errorLabel: UILabel!
@IBOutlet weak var emailTextfield: UITextField!
@IBOutlet weak var passwordTextfield: UITextField!



@IBAction func registerPressed(_ sender: UIButton) {
    
    if let email = emailTextfield.text, let password = passwordTextfield.text {
        Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
            if let e = error {
                self.errorLabel.text = e.localizedDescription
            } else {
                self.performSegue(withIdentifier: "RegisterToChat", sender: self)
            }
        }
    }
  }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Moonstone
  • 1
  • 1

1 Answers1

1

The text property of UITextFields is not nil when the text field is empty. You need to check the isEmpty property of textField.text to check for that.

if let email = emailTextField.text, let password = passwordTextField.text, !email.isEmpty, !password.isEmpty { ...

Actually, UITextField.text can never be nil. The property is only Optional due to it being a legacy, Objective-C class. For more information on this, check Why is UITextField.text an Optional.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116