我需要在显示下一个视图之前验证登录身份验证。我知道" print"这些错误,但我需要返回false以防止segue。
我使用prepare函数从webservice获取JSON数据:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
do {
let username = txtUsername.text
let password = txtPassword.text
// webservice
let urlString = "http://myweb.com/api/user/username/\(username!)/password/\(password!)/key/xxx"
if let url = URL(string: urlString) {
let datosWeb = try Data(contentsOf: url)
// json
let jsonObject = try JSONSerialization.jsonObject(with: datosWeb, options: [])
if let temp = jsonObject as? [[String:String]] {
self.items = temp
}
} else {
print("URL not exists") // I need a return false
}
} catch {
print("Error obtaining data") // I need a return false
}
}
因此,准备功能没有bool输出。我有一个带有shouldPerformSegue的文本字段验证,但我需要一个类似的函数来验证"在"之后。
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if (txtUsername.text?.isEmpty)! || (txtPassword.text?.isEmpty)! {
showAlert(title: "Error", message: "Please, complete the fields.")
return false
}
else {
return true
}
}
答案 0 :(得分:2)
您可能有一个登录按钮来触发登录,因此您应该在登录按钮逻辑中执行此操作:
func loginClicked() {
if (txtUsername.text?.isEmpty)! || (txtPassword.text?.isEmpty)! {
showAlert(title: "Error", message: "Please, complete the fields.")
}
else {
self.performSegue(withIdentifier: "SegueID", sender: nil)
}
}
无需传递bool变量。如果您的验证通过了您的segue。