我有一个检查登录是否正确的函数。如果没问题,我会显示下一个屏幕。如果用户不正确,我取消segue并显示警告。
出现问题是因为当我在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
}
提前致谢。
答案 0 :(得分:5)
只需使用ViewController A到ViewController B即可使用segue,假设segue的名称为mySegue1, 假设点击登录按钮。检查一切是否正确。如果它是正确的那么,
self.performSegueWithIdentifier("mySegue1", sender: self)