由于未捕获的异常Swift终止应用程序

时间:2017-07-12 17:26:41

标签: ios swift xcode

我目前正在使用登录身份验证,以及使用firebase注册。我现在想让我的用户登录,这是有效的。我面临的问题是当我退出时,我尝试重新登录并收到此错误: //由于未捕获的异常而终止应用' NSInvalidArgumentException',原因:' Receiver()没有带标识符的segue' chatRoom'' \ 这没有意义,因为当我第一次登录时,它会使用chatRoom segue将我带到ChatRoom Viewcontroller。

这是我的代码:

 func handleLogin() {
        guard let email = emailTextField.text, let password = passwordTextField.text
            else {
            print("Form is not valid")
            return
        }
        Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in

            if error != nil {
                print(error!.localizedDescription)
                self.loginErrorAlert("Error!", message: "Username or password incorrect, please try again.")
                return
            }
            // successfully logged in our user and keep user logged in until they logout
            if Auth.auth().currentUser != nil {
                UserDefaults.standard.set(Auth.auth().currentUser!.uid, forKey: "loggedIn")
                UserDefaults.standard.synchronize()
             self.performSegue(withIdentifier: "chatRoom", sender: self)// this is the error I am facing
            }
        })
    }
    func handleRegister() {
        guard let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text else {
            print("Form is not valid")
            signupErrorAlert("Error!", message: "Could not be Registered at this time, please try again.")
            return
        }
        Auth.auth().createUser(withEmail: email, password: password, completion: { (user: User?, error) in

            if error != nil {
                print(error!.localizedDescription)
                self.loginErrorAlert("Error!", message: "Could not be Registered at this time, please try again later.")
                return
            }

            guard let uid = user?.uid else {
                return
            }
            // successfully authenticated user and keep logged in until they logout
            let ref = Database.database().reference(fromURL: "https://boccighub.firebaseio.com/")
            let usersReference = ref.child("users").child(uid)
            let values = ["name": name, "email": email]
            usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in

                if err != nil {
                    print(err!.localizedDescription)
                    return
                }
                if Auth.auth().currentUser != nil {
                    UserDefaults.standard.set(Auth.auth().currentUser!.uid, forKey: "loggedIn")
                    UserDefaults.standard.synchronize()
                    self.performSegue(withIdentifier: "chatRoom", sender: self)
                }
            })
        })
    }
    // User logged out
    @IBAction func handleLogout(_ sender: Any) {
        do {
            try Auth.auth().signOut()
            print("user signedout")

            if Auth.auth().currentUser == nil {
                print("No user, key removed")
                UserDefaults.standard.removeObject(forKey: "loggedIn")
                UserDefaults.standard.synchronize()
                print("User logged out")
                let loginController = LoginViewController()
                present(loginController, animated: true, completion: nil)
            }
        } catch let logoutError {
            print(logoutError)
        }
    }

1 个答案:

答案 0 :(得分:0)

固定。我删除了let loginController = LoginViewController(), 我创建了一个新的segue,现在做self.performSegue(withIdentifier:" logOut",sender:self),效果很好