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

  • 本文关键字:Segue 是否 ios swift segue
  • 更新时间 :
  • 英文 :


我试图仅在凭据正确时才执行 segue。否则,应该有一个警报,指出"凭据不正确"。当我运行应用程序时,segue 正在执行并转到下一个视图,凭据是否正确/不正确。在故事板中,我以模式连接到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。

最新更新