Search code examples
swiftxcodesegueviewcontroller

Register screen won't successfully segue to a new view controller.


Good afternoon. I am trying to implement a register feature into my application. When I successfully register, it will segue to the main view controller, however, it will just redirect back to the register view controller. Can someone please let me know what I am doing wrong?

Auth.auth().createUser(withEmail: email, password: password) { (User, error) in

        if error != nil {
            print("error")
            return
        }else{
            let MainView = UINavigationController(rootViewController: mainViewController())
            self.present(MainView, animated: true, completion: nil)
        }
        self.defaults.set(false, forKey: "user is logged in")

        let userID = Auth.auth().currentUser!.uid

        let ref = Database.database().reference(fromURL: "https://odd-jobs-llc-f854a.firebaseio.com/")
        let usersReference = ref.child("users").child(userID)
        let values = ["first name": firstname, "last name": lastname, "phone number": phonenumber,
        "email": email]
        usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in

            if err != nil {
                print("err")
                return
            }

            self.dismiss(animated: true, completion: nil)
        })


    }
}

Solution

  • This will present the VC

     let MainView = UINavigationController(rootViewController: mainViewController())
     self.present(MainView, animated: true, completion: nil)
    

    and this

     self.dismiss(animated: true, completion: nil)
    

    will dismiss it , you may mean something like this

    Auth.auth().createUser(withEmail: email, password: password) { (User, error) in
    
            if error != nil {
                print("error")
                return
            } 
    
            self.defaults.set(false, forKey: "user is logged in")
    
            let userID = Auth.auth().currentUser!.uid
    
            let ref = Database.database().reference(fromURL: "https://odd-jobs-llc-f854a.firebaseio.com/")
            let usersReference = ref.child("users").child(userID)
            let values = ["first name": firstname, "last name": lastname, "phone number": phonenumber,
            "email": email]
            usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
    
                if err != nil {
                    print("err")
                    return
                }
    
                 let MainView = UINavigationController(rootViewController: mainViewController())
                 self.present(MainView, animated: true, completion: nil)
    
    
            })
    
    
        }
    }