在登录Swift / Parse

时间:2015-12-13 03:58:14

标签: ios swift

我有一个登录视图控制器,它应该阻止用户通过名为" toMasterTab"的segue转换到下一个视图控制器。我认为逻辑可能是错误的 - 如果用户输入了正确的凭证并且不是空的,则转换正常,但如果用户没有输入凭证(nil)并输入了错误的凭证,那么它应该阻止segue。到目前为止,我只能弹出UIAlertView,但除此之外,我无法解决这个问题......

    @IBAction func loginButton(sender: AnyObject) {
    let RedPlanetUser = RPUsername.text
    let RedPlanetUserPassword = RPUserPassword.text

    PFUser.logInWithUsernameInBackground(RedPlanetUser!, password: RedPlanetUserPassword!) {
        (user: PFUser?, error: NSError?) -> Void in

        if user != nil {
            // Do stuff after successful login
            self.performSegueWithIdentifier("toMasterTab", sender: self)
            print("User logged in successfully")
        } else {
            // Login failed
            print("User log in failed")
            // Present alert
            var alert = UIAlertView(title: "Login Failed",
                message: "The username and password do not match.",
                delegate: self,
                cancelButtonTitle: "Try Again")
                alert.show()

            func shouldPerformSegueWithIdentifier(identifier: String!, object: AnyObject) -> Bool {
                let identifier = "toMasterTab"
                    // prevent segue
                return false
            }

        }

    }

}

1 个答案:

答案 0 :(得分:1)

我相信你应该重写

override func shouldPerformSegueWithIdentifier

问题是segue已连接到按钮,因此即使条件不满足,它也会自动执行segue。我将segin从VC1连接到VC2并在满足条件时使用以下代码,并且在条件错误时不调用segue:

    self.performSegueWithIdentifier("toMasterTab", sender: self)