我有一个多步骤用户注册表格,在第一页上,我正在对照数据库检查文本字段中的电子邮件地址,以查看其是否存在。我有一个针对数据库进行检查并返回一个布尔值的函数,该布尔值在segue之后运行但正在运行。我想确保在执行segue之前检查此值。
这是验证代码,然后转到下一个屏幕:
@IBAction func nextButtonClicked(_ sender: Any) {
// Validation
if (txtUsername.text!.isEmpty) {
helper.displayMessage(vc: self, userMessage: "You must enter a username.", dismiss: false)
return
}
if (txtEmail.text!.isEmpty) {
helper.displayMessage(vc: self, userMessage: "You must enter a valid email address.", dismiss: false)
return
}
// This is the function that is running post-segue
doesEmailExist(email: txtEmail.text!, userCompletionHandler: { doesExist, error in
if let doesExist = doesExist {
if (doesExist == true) {
self.helper.displayMessage(vc: self, userMessage: "This email is already registered.", dismiss: false)
return
}
}
})
if (txtPassword.text!.count < 6) {
helper.displayMessage(vc: self, userMessage: "Password must be at least 6 characters.", dismiss: false)
return
}
if (txtPassword.text! != txtConfirmPassword.text!) {
helper.displayMessage(vc: self, userMessage: "Passwords must match.", dismiss: false)
return
}
// Set global vars
users.username = txtUsername.text
users.email = txtEmail.text
users.password = txtPassword.text
DispatchQueue.main.async {
self.performSegue(withIdentifier: "goToPage2", sender: nil)
}
}
这是检查电子邮件是否存在的功能:
func doesEmailExist(email: String, userCompletionHandler: @escaping (Bool?, Error?) -> Void) {
print(email)
let myURL = URL(string: "https://www.mysite1.org/checkIfEmailExists.php")
var request = URLRequest(url:myURL!)
request.httpMethod = "POST"
let postString = email
print(postString)
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil {
self.helper.displayMessage(vc: self, userMessage: "Could not perform this request, try later")
print("error=\(String(describing: error))")
return
}
// convert server side response to NSDictionary object
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let msg = parseJSON["status"] as? String
if (msg == "error") {
self.doesEmailExist = true
userCompletionHandler(true, nil)
print("MSG=error \(self.doesEmailExist)")
} else {
self.doesEmailExist = false
userCompletionHandler(false, nil)
print("MSG != error \(self.doesEmailExist)")
}
} else {
userCompletionHandler(nil, error)
self.helper.displayMessage(vc: self, userMessage: "Couldn't do it, parseJSON = json", dismiss: true)
}
} catch {
self.helper.displayMessage(vc: self, userMessage: "Couldn't do it, do stmt", dismiss: true)
print(error)
}
})
task.resume()
}
答案 0 :(得分:0)
您可以在doesEmailExist
方法的回调中执行此操作,如果不存在电子邮件,请检查其他步骤,并且回调返回false(表示用户不存在),然后在其他步骤也有效的情况下执行segue:
doesEmailExist(email: txtEmail.text!, userCompletionHandler: { doesExist, error in
if let doesExist = doesExist {
if (doesExist == true) {
self.helper.displayMessage(vc: self, userMessage: "This email is already registered.", dismiss: false)
return
} else {
if (txtPassword.text!.count < 6) {
self.helper.displayMessage(vc: self, userMessage: "Password must be at least 6 characters.", dismiss: false)
return
}
if (txtPassword.text! != txtConfirmPassword.text!) {
self.helper.displayMessage(vc: self, userMessage: "Passwords must match.", dismiss: false)
return
}
// Set global vars
self.users.username = txtUsername.text
self.users.email = txtEmail.text
self.users.password = txtPassword.text
DispatchQueue.main.async {
self.performSegue(withIdentifier: "goToPage2", sender: nil)
}
}
}
})
编译器会警告您对变量使用self
,使用self编辑代码,然后编译并可以使用。您可能还想使用[weak self]
来防止内存泄漏。