我目前正在努力解决当我的segue被触发时,下一个ViewController上的内容消失或需要一段时间才能加载的问题。
这是我触发segue的代码:
userCount = count
if userCount != -1{
self.performSegue(withIdentifier: "GoToWelcome", sender: self)
}
if userCount == -1{
self.LoadSign.isHidden = true
self.createAlert(title: "Error!", message: "Incorrect E-Mail, Please try again")
}
这是两个视图控制器的屏幕截图,当按下“登录”按钮时,数据在数据库中被检查,如果它是正确的,则会触发segue: IMAGE
我的WelcomeViewController代码(尚未编辑):
import UIKit
class WelcomeViewController: UIViewController {
@IBOutlet weak var welcomeMessage: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
这是我的LoginViewController的代码(我知道这很乱)
import UIKit
class LoginScreenController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var LoadSign: UIActivityIndicatorView!
@IBOutlet weak var EMail: UITextField!
@IBOutlet var LoginScreenView: UIView!
@IBAction func LoginButton(_ sender: UIButton) {
LoadSign.isHidden = false
let email = EMail.text
let SQLurl = URL(string:"[myUrl]")
let task = URLSession.shared.dataTask(with: SQLurl!) { (data, responce, error) in
if error != nil
{
print("error")
}
else
{
if let content = data
{
do
{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
var notInDB = false
var count = 0
var myJsonCount = myJson[count] as! NSDictionary
var EmailCount = (myJsonCount["txtEmailAddress"]) as! NSString
var userCount = 0
while email != EmailCount as String && notInDB == false{
count = count+1
myJsonCount = myJson[count] as! NSDictionary
EmailCount = (myJsonCount["txtEmailAddress"]) as! NSString
if count == (myJson.count-1){
print("Error! E-Mail not found in database")
notInDB = true
count = -1
}
}
userCount = count
if userCount != -1{
self.performSegue(withIdentifier: "GoToWelcome", sender: nil)
}
if userCount == -1{
self.LoadSign.isHidden = true
self.createAlert(title: "Error!", message: "Incorrect E-Mail, Please try again")
}
}
catch
{
}
}
}
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
EMail.delegate = self
EMail.adjustsFontSizeToFitWidth = true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func createAlert (title: String, message: String)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Try Again", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
}
有什么理由说明会发生这种情况吗? 谢谢!
答案 0 :(得分:0)
我认为问题可能是您从不同的线程使用UIKit。 UIKit调用performSegue
(可能是self.createAlert
调用的内容,虽然我从你给出的代码中无法知道)应该只从主线程中调用。
我的建议是在dataTask的完成处理程序中处理与您的请求相关的内容,然后将控制权传递回主线程以处理其余部分。这是基于您发送的代码的草图。请注意,我们只是获取响应数据并在dataTask的后台线程中解析JSON,然后将控制传递给主线程上的函数来完成剩下的工作:
@IBAction func LoginButton(_ sender: UIButton) {
LoadSign.isHidden = false
let email = EMail.text
let SQLurl = URL(string:"[myUrl]")
let task = URLSession.shared.dataTask(with: SQLurl!) { (data, responce, error) in
if error != nil
{
print("error")
}
else
{
do {
// parse the JSON
if let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSArray {
DispatchQueue.main.async { self.handleLoginData(myJson) }
} catch {
print("JSON Parse error")
}
}
}
task.resume()
}
func handleLoginData(myJson: NSArray) {
do {
var notInDB = false
var count = 0
var myJsonCount = myJson[count] as! NSDictionary
var EmailCount = (myJsonCount["txtEmailAddress"]) as! NSString
var userCount = 0
while email != EmailCount as String && notInDB == false {
count = count+1
myJsonCount = myJson[count] as! NSDictionary
EmailCount = (myJsonCount["txtEmailAddress"]) as! NSString
if count == (myJson.count-1) {
print("Error! E-Mail not found in database")
notInDB = true
count = -1
}
}
userCount = count
if userCount != -1 {
self.performSegue(withIdentifier: "GoToWelcome", sender: nil)
} else {
self.LoadSign.isHidden = true
self.createAlert(title: "Error!", message: "Incorrect E-Mail, Please try again")
}
}
catch
{
}
}
我必须稍微使用代码来使其工作,但请注意它如何通过请求将请求的数据处理留下,并将问题域的工作分离到另一个函数。使用异步块时,尝试将任何特定块中的所有工作完成,只与一个任务相关。