如果凭据正确,我试图只执行segue。否则应该有一个警告声明“不正确的凭据”。当我运行应用程序时,segue正在执行并转到下一个视图,无论凭据是否正确/不正确。在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()
}}
答案 0 :(得分:1)
您应该实现shouldPerformSegueWithIdentifier:sender:
方法来控制是否要执行segue。