0

Line where error is:

self.passwordField.delegate = self

Code from the button:

 @IBAction func unwindToRed(_ sender: Any) {

    do {
                   try Auth.auth().signOut()
                   let ViewController1 = ViewController()
                   let ViewNavigationController = UINavigationController(rootViewController: ViewController1)
                   self.present(ViewNavigationController, animated: true, completion: nil)
               } catch let err {
                   print(err)
               }

}

This is the relevant homepage code:

class ViewController: UIViewController, UITextFieldDelegate {



@IBOutlet weak var emailField: UITextField!

@IBOutlet weak var passwordField: UITextField!

var userUID: String!
var databaseRef: DatabaseReference!

override func viewDidLoad() {

    super.viewDidLoad()

    databaseRef = Database.database().reference()


    self.passwordField.delegate = self
    self.emailField.delegate = self

    emailField.attributedPlaceholder = NSAttributedString(string: "Email",
    attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray])

    passwordField.attributedPlaceholder = NSAttributedString(string: "Password",
    attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray])



}
 override func viewDidAppear(_ animated: Bool) {
    if let _ = KeychainWrapper.standard.string(forKey: "uid") {
        self.performSegue(withIdentifier: "tohome", sender: nil)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
     @IBAction func signInPressed(_ sender: Any) {

   Auth.auth().createUser(withEmail: (emailField.text ?? ""), password: (passwordField.text ?? ""))  { (user, error) in
       if let _eror = error {
           //something bad happning
           print(_eror.localizedDescription )
        let alert = UIAlertController(title: "Error", message: "Invalid Entry or Duplicate.", preferredStyle: UIAlertController.Style.alert)
                             let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
                             alert.addAction(action)
                             self.present(alert, animated: true, completion: nil)

       }else{
           //user registered successfully
           print(user as Any)
                 if let userID = user?.uid {
                                           KeychainWrapper.standard.set((userID), forKey: "uid")

                                           let databaseRef = Database.database().reference()


databaseRef.child("people").child(userID).child("users").setValue(self.emailField.text!)

databaseRef.child("people").child(userID).child("postID").setValue(userID)

                                           self.performSegue(withIdentifier: "tohome", sender: nil)
                                       }

       }
    }
}

 @IBAction func loginInPressed(_ sender: Any) {
 Auth.auth().signIn(withEmail: (emailField.text ?? ""), password: (passwordField.text ?? ""))  { (user, error) in
 if let _eror = error {
               //something bad happning
               print(_eror.localizedDescription )
            let alert = UIAlertController(title: "Error", message: "Incorrect Email or Password.", preferredStyle: UIAlertController.Style.alert)
                                 let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
                                 alert.addAction(action)
                                 self.present(alert, animated: true, completion: nil)

           }else{
               //user registered successfully
               print(user as Any)
                    if let userID = user?.uid {
                        KeychainWrapper.standard.set((userID), forKey: "uid")
                        self.performSegue(withIdentifier: "tohome", sender: nil) }

           }
        }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

0

Problem is hiding in this line:

@IBAction func unwindToRed(_ sender: Any) {
    do {
          try Auth.auth().signOut()
          let ViewController1 = ViewController() // <-- This is the problem
          let ViewNavigationController = UINavigationController(rootViewController: ViewController1)
          self.present(ViewNavigationController, animated: true, completion: nil)
     } catch let err {
          print(err)
     }

}

Because you are using storyboards to create your views, you should instantiate your view controller from storyboard. To do this right please refer to Creating View Controllers from Storyboard.

If you're new to iOS development or you don't know why this is required please refer to this post which will explain instantiating view controller from storyboard vs. creating new instance.

lacefarin
  • 1,018
  • 2
  • 13
  • 18