无论凭据是否正确,都会触发 Segue

Segue is triggered whether credentials are correct or not

我正在尝试仅在凭据正确的情况下执行 segue。否则应该有一个警告说明 "incorrect credentials"。当我 运行 应用程序正在执行 segue 并转到下一个视图时,凭据是否 correct/incorrect。在 Storyboard 中,我已模态连接到 AccountViewController。为什么会发生这种情况?

class ViewController: UIViewController, NSURLConnectionDelegate {


@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!

var token: NSString = ""


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        var accountViewController = segue.destinationViewController as AccountViewController
        accountViewController.token = self.token

}

private let api_key = ""

func logIn() {


    var email: NSString = emailTextField.text
    var pass: NSString = passwordTextField.text

    // Remove characters from the custom character set with the custom set

    let customEncodedSet: NSMutableCharacterSet = NSCharacterSet.URLHostAllowedCharacterSet().mutableCopy() as NSMutableCharacterSet
    customEncodedSet.removeCharactersInString("!*'();:@&=+$,/?%#[]")

        var encodedEmail = email.stringByAddingPercentEncodingWithAllowedCharacters(customEncodedSet)
        var encodedPass = pass.stringByAddingPercentEncodingWithAllowedCharacters(customEncodedSet)


    var url = "https://www.photoshelter.com/psapi/v3/mem/authenticate?api_key=\(api_key)&email=\(encodedEmail!)&password=\(encodedPass!)&mode=token"


    var baseURL:NSURL? = NSURL(string: url)
    var request: NSMutableURLRequest? = NSMutableURLRequest(URL: baseURL!)
    var session = NSURLSession.sharedSession()
    var task = session.dataTaskWithRequest(request!, completionHandler: { (data, response, error) -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in


            if response == nil {
                UIAlertView(title: "PhotoShelter", message: "No internet connection", delegate: nil, cancelButtonTitle: "Cancel").show()
            } else {
                var responseObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil)
                var status: AnyObject? = responseObject?.objectForKey("status")

                if status as String != "ok" {
                    UIAlertView(title: "PhotoShelter", message: "Invalid Credentials", delegate: nil, cancelButtonTitle: "OK").show()

                } else {

                    var tokenString: AnyObject? = responseObject?.objectForKey("data")
                    self.token = tokenString?.objectForKey("token") as NSString
                    self.performSegueWithIdentifier("loginSuccessSegue", sender: self)
                }

            }

        })
    })
    task.resume()
}


@IBAction func signInPressed(sender: AnyObject) {

    if self.emailTextField.text == "" || self.passwordTextField.text == "" {
        UIAlertView(title: "PhotoShelter", message: "Empty Username or Password", delegate: nil, cancelButtonTitle: "OK").show()


    } else {
        logIn()
    }}

您应该实现 shouldPerformSegueWithIdentifier:sender: 方法来控制是否要执行 segue。