我如何修复尝试在视图不在 window 层次结构中的 ViewController 上呈现 UIAlertController
How can i fix attempt to present UIAlertController on ViewController whose view is not in the window hierarchy
我正在尝试将警报作为一种在登录我的应用程序时显示潜在错误的方式。
我的代码的不同部分有几个警报。 None 他们的工作。他们不显示和打印:
Attempt to present on whose view is not in the window hierarchy!
我看到很多不同的帖子都有同样的问题,他们都通过 viewDidAppear
解决了问题。我试过了,但由于它不仅仅是一个警报,而且由于我使用一个函数来调用每个警报,所以该帮助不起作用。
这是一些代码:
func alerts(title: String, message: String) {
let alert = UIAlertController(title:"Oops...", message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title:"Ok", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
@IBAction func loginPressed(_ sender: Any) {
if emailField.text == "", pwField.text == "" {
alerts(title: "Oops...", message: "Please fill out the fields.")
accessGiven = false
} else {
accessGiven = true
}
// if emailField.text == "", pwField.text != "" || emailField.text != "", pwField.text == "" {
// alerts(title: "Oops...", message: "Please fill out all fields.")
// accessGiven = false
// } else {
// accessGiven = true
// }
Auth.auth().fetchProviders(forEmail: self.emailField.text!, completion: { (stringArray, error) in
if error != nil {
print(error!)
self.alerts(title: "Oops...", message: "Something went wrong, please try again")
self.accessGiven = false
} else {
if stringArray == nil {
print("No active account using this email.")
self.alerts(title: "Oops...", message: "There is no active account using this E-Mail.")
self.accessGiven = false
//self.performSegue(withIdentifier: "segue.SignUp.toTabBar", sender: nil)
} else {
print("There is an active account using this E-mail")
self.accessGiven = true
}
}
})
Auth.auth().signIn(withEmail: emailField.text!, password: pwField.text!) { (user, error) in
if let error = error {
print(error.localizedDescription)
self.alerts(title: "Oops...", message: "Something went wrong, please try again")
self.accessGiven = false
} else {
self.accessGiven = true
}
if let user = user {
}
}
if accessGiven == true {
self.performSegue(withIdentifier: "segue.LogIn.toTabBar", sender: nil)
}
}
如果您的 accessGiven 值为真,则您正在对另一个 viewController 执行 segue。因此,您当前的 viewController 不再是 window 层次结构的一部分。然后您尝试使用 self.present(alert, animated: true, completion: nil)
从 fetchProviders 和 signIn 函数的 completionBlocks 中呈现警报控制器,其中 self 是您的 viewController 不再是 window 层次结构的一部分。这就是抛出错误的原因。为了确保,只需注释您的 performSegue 并尝试相同的代码。希望对你有帮助。
我正在尝试将警报作为一种在登录我的应用程序时显示潜在错误的方式。 我的代码的不同部分有几个警报。 None 他们的工作。他们不显示和打印:
Attempt to present on whose view is not in the window hierarchy!
我看到很多不同的帖子都有同样的问题,他们都通过 viewDidAppear
解决了问题。我试过了,但由于它不仅仅是一个警报,而且由于我使用一个函数来调用每个警报,所以该帮助不起作用。
这是一些代码:
func alerts(title: String, message: String) {
let alert = UIAlertController(title:"Oops...", message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title:"Ok", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
@IBAction func loginPressed(_ sender: Any) {
if emailField.text == "", pwField.text == "" {
alerts(title: "Oops...", message: "Please fill out the fields.")
accessGiven = false
} else {
accessGiven = true
}
// if emailField.text == "", pwField.text != "" || emailField.text != "", pwField.text == "" {
// alerts(title: "Oops...", message: "Please fill out all fields.")
// accessGiven = false
// } else {
// accessGiven = true
// }
Auth.auth().fetchProviders(forEmail: self.emailField.text!, completion: { (stringArray, error) in
if error != nil {
print(error!)
self.alerts(title: "Oops...", message: "Something went wrong, please try again")
self.accessGiven = false
} else {
if stringArray == nil {
print("No active account using this email.")
self.alerts(title: "Oops...", message: "There is no active account using this E-Mail.")
self.accessGiven = false
//self.performSegue(withIdentifier: "segue.SignUp.toTabBar", sender: nil)
} else {
print("There is an active account using this E-mail")
self.accessGiven = true
}
}
})
Auth.auth().signIn(withEmail: emailField.text!, password: pwField.text!) { (user, error) in
if let error = error {
print(error.localizedDescription)
self.alerts(title: "Oops...", message: "Something went wrong, please try again")
self.accessGiven = false
} else {
self.accessGiven = true
}
if let user = user {
}
}
if accessGiven == true {
self.performSegue(withIdentifier: "segue.LogIn.toTabBar", sender: nil)
}
}
如果您的 accessGiven 值为真,则您正在对另一个 viewController 执行 segue。因此,您当前的 viewController 不再是 window 层次结构的一部分。然后您尝试使用 self.present(alert, animated: true, completion: nil)
从 fetchProviders 和 signIn 函数的 completionBlocks 中呈现警报控制器,其中 self 是您的 viewController 不再是 window 层次结构的一部分。这就是抛出错误的原因。为了确保,只需注释您的 performSegue 并尝试相同的代码。希望对你有帮助。