无论凭据是不是正确,都会触发 Segue
Posted
技术标签:
【中文标题】无论凭据是不是正确,都会触发 Segue【英文标题】:Segue is triggered whether credentials are correct or not无论凭据是否正确,都会触发 Segue 【发布时间】:2015-01-22 04:42:34 【问题描述】:我试图仅在凭据正确的情况下执行 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()
【问题讨论】:
在您的故事板中,您的登录按钮是否直接链接到下一个视图控制器? 如果是这样,也许这个答案将有助于解释你应该做什么:***.com/a/20690072/2274694 【参考方案1】:您应该实现shouldPerformSegueWithIdentifier:sender:
方法来控制是否要执行segue。
【讨论】:
@LyndseyScott,我只看到prepareForSegue
方法,我错了吗?
是的,shouldPerformSegueWithIdentifier:sender: 在 login() 的 dataTaskWithRequestBlock 中,如果状态 == ok 则调用
我也看不到shouldPerformSegueWithIdentifier
,但是块中的逻辑应该足够了-Lyndsey 的其他评论可能是问题所在-除了@ 之外,还有从按钮到segue 的直接链接IBA动作
哦,是的,你们都说得对。没注意,不好意思。我的意思是 performSegueWithIdentifier。以上是关于无论凭据是不是正确,都会触发 Segue的主要内容,如果未能解决你的问题,请参考以下文章