gpt4 book ai didi

ios - 防止 segue 在函数运行之前执行

转载 作者:行者123 更新时间:2023-11-28 07:32:28 26 4
gpt4 key购买 nike

我有一个多步骤用户注册表,在第一页上,我根据数据库检查文本字段中的电子邮件地址,看它是否存在。我有一个函数可以检查数据库并返回一个正在运行但在 segue 之后运行的 bool 值。我想确保在执行 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()

}

最佳答案

您可以在 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] 来防止内存泄漏。

关于ios - 防止 segue 在函数运行之前执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54296759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com