【发布时间】:2018-06-06 03:25:27
【问题描述】:
更具体地说,我得到的错误警告是:
Attempt to present UIAlertController whose view is not in the window hierarchy!
我目前正在创建一个注册页面,要求用户填写一些具体的详细信息,例如姓名、国家、电子邮件、密码等。为了确保用户提供所有相关信息,我正在尝试编写代码来发送警报如果用户没有提供所有信息。我已经编写了从 stakeoverflow 获得帮助的代码。
问题:每当用户将任何字段留空时,它都不会显示警报,并且默认情况下会执行将用户带到登录页面的 segue。这是我第一次创建警报,因此不要出什么问题(我相信我 95% 的代码都已到位)
谁能帮忙?
@IBAction func signUpPressed(_ sender: Any) {
if nameText.text!.isEmpty || genderText.text!.isEmpty || countryText.text!.isEmpty || yourSchool.text!.isEmpty || yourClass.text!.isEmpty {
print("Please fill all fields")
//my code is printing above error in the Xcode console but the below code is not working
//setting error message if not all fiels filled
let alertController = UIAlertController(title: "Error", message: "Please fill all fields", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
else {
Auth.auth().createUser(withEmail: yourEmail.text!, password: yourPassword.text!) { (user, error) in
if error != nil {
///print errror message
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
} else {
print("You have successfully signed up")
self.performSegue(withIdentifier: "JoinUs2SignPage", sender: self)
//updating user information
let userID = Auth.auth().currentUser!.uid
let usertype: String = "Student"
self.ref.child("users").child(userID).setValue(["usertype": usertype ,"username": self.nameText.text!, "usergender": self.genderText.text!, "usercountry": self.countryText.text!, "userschool": self.yourSchool.text!, "userclass": self.yourClass.text!,])
}
}
}
【问题讨论】:
-
你用断点测试过这个吗?我的意思是没有其他代码做同样的事情或执行 segue 的故事板连接?
-
为什么
yourSchool.text被检查了两次? -
@vadish 在这里两次不会出问题
-
@Sh_Khan 和
== ""和.isEmpty没有区别。 -
@vadian swift 有问题
标签: ios swift xcode uialertcontroller uialertaction