我有一个检查登录是否正确的功能。如果没问题,我会显示下一个屏幕。如果用户不正确,我会取消 segue 并显示 alert 。
出现问题是因为当我在 shouldPerformSegueWithIdentifier 函数中调用此函数时,我给一个布尔变量赋值(如果用户正确与否),然后 shouldPerformSegueWithIdentifier 的返回值就是这个布尔值。问题是它不采用该值并保持默认值。这是我的代码:
override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
var userIsCorrect = false //THIS IS THE BOOLEAN
if identifier == "fromLogInToGetIn" {
self.loginRequest("http://myurl.com",
withParams: ["email":"email@email.com","password":"password"])
{
(succeeded: Bool, msg: String) -> () in
var alert = UIAlertView(title: "Success!", message: msg, delegate: nil, cancelButtonTitle: "Okay.")
if(succeeded) {
if msg == "0"
{
userIsCorrect = false // BOOLEAN DOES NOT TAKE THE VALUE
alert.title = "Error"
alert.message = "Incorrect user"
}
else
{
userIsCorrect = true // BOOLEAN DOES NOT TAKE THE VALUE
}
}
else {
userIsCorrect = false // BOOLEAN DOES NOT TAKE THE VALUE
alert.title = "Error"
alert.message = "Something is wrong"
}
// Move to the UI thread
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// Show the alert
if userIsCorrect == false
{
alert.show()
}
})
}
}
return userIsCorrect // ALWAYS RETURN DEFAULT VALUE
}
提前致谢。