如果用户名和密码错误SWIFT,请停止继续下一个控制器

时间:2015-04-21 18:04:47

标签: xcode swift parsing ios8 xcode6

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool
    {
        if (identifier == "gotoLogin")
        {
            if txtUsername.text.isEmpty || txtPassword.text.isEmpty
            {
                let alertView = UIAlertView(title: "Error", message: "Cannot blank", delegate: self, cancelButtonTitle: "OK")
                alertView.show()
                return false
            }
            else if txtUsername.text != "" && txtPassword.text != ""
            {
                activityIndicator.startAnimating()
                PFUser.logInWithUsernameInBackground(txtUsername.text, password: txtPassword.text, block: { (user:PFUser?, error:NSError?) -> Void in
                    if(error == nil)
                    {
                        self.activityIndicator.stopAnimating()
                        self.performSegueWithIdentifier("gotoLogin", sender: self)
                    }
                    else
                    {
                        self.activityIndicator.stopAnimating()
                        var errorCode = error?.code
                        switch errorCode!
                        {
                        case 101:
                            var alertView = UIAlertView(title: "Incorrect Username or Password", message: "Try Again!", delegate: self, cancelButtonTitle: "OK")
                            alertView.show()
                            break
                        default:
                            break
                        }
                    }
                })
                return true
            }
        }
        return true
    }

This picture is showing it proceeds to next controller even with the wrong username and password, and the alertview is pop out in next controller instead of in the login page.

如果用户名和密码不正确,我已经进行了验证。因此,如果它们都不正确,则应该保留在登录页面中以显示警报视图。但是,在这种情况下,它进入下一个控制器,然后只显示警报视图。如何保持登录页面甚至用户名和密码不正确,而不是继续下一个控制器。

1 个答案:

答案 0 :(得分:0)

Milo是正确的,处理这种情况的最佳方法是在用户输入正确的详细信息之前不要激活segue。

检查提交表单,通过触发segue处理成功,通过发出警报来处理失败。

像这样安排你的代码:

 func onSubmitButton(sender: AnyObject){
     if username !="" && password !="" {
         success = checkusername(username,password)
         //if this is an http request you probably want to do a delegate pattern 
         //and create a callback instead- just create 2 delegate functions-
        // one for success and one for fail and pass self of vc to http request)
         if success{
              performSegue();
         }else{
              clearForm();
              throwAlert()
         }
    }

这样的事情就是你追求的模式。