gpt4 book ai didi

ios - 尝试呈现 AlertController 时的窗口层次结构消息

转载 作者:行者123 更新时间:2023-11-29 00:11:58 24 4
gpt4 key购买 nike

我有一个 swift 文件用于管理与 Firebase 的连接(注册、登录、保存数据)。在此文件中,使用一些 AlertController 根据来自 Firebase 的消息通知用户(错误的电子邮件格式,已使用的电子邮件)。问题是当某些 alerController 必须出现时,他们会收到此消息:

Warning: Attempt to present *** on *** whose view is not in the window hierarchy!

Signup ViewController 是当前 View ,该 View 链接到另一个特定文件。一般来说,我在 Stackoverflow 和 Google 上阅读了很多关于“windows hierarchy”的内容,并尝试了不同的东西。

我在注册时使用了类似的代码时遇到了同样的问题。我从这种方式开始展示 alertController :

present(alerInvalidEmail, animated: true, completion: nil)

致那个人:

UIApplication.shared.keyWindow?.rootViewController?.present(alertInvalidEmail, animated: true, completion: nil)

现在它工作正常,但它不适用于我的注册部分。 我想它与当前的 ViewController 有关,但找不到我的方法。

这是我的网络文件中的代码:

struct NetworkingService {

var databaseRef: FIRDatabaseReference! {
return FIRDatabase.database().reference()
}
var storageRef: FIRStorageReference! {
return FIRStorage.storage().reference()
}


func signUp(email: String, pseudo:String, password: String, data: NSData!){

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in

if error != nil {
print(error!.localizedDescription)


if let SignUperrCode = FIRAuthErrorCode(rawValue: error!._code) {
switch SignUperrCode {

case .errorCodeInvalidEmail:
let alertInvalidEmail = UIAlertController(title: "Email validation", message: "You have entered an malformed adress", preferredStyle: .alert)
alertInvalidEmail.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction) in
print("Invalid email")
}))

UIApplication.shared.keyWindow?.rootViewController?.present(alertInvalidEmail, animated: true, completion: nil)


case .errorCodeEmailAlreadyInUse:
let alertUsedEmail = UIAlertController(title: "Email validation", message: "This email is already used", preferredStyle: .alert)
alertUsedEmail.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction) in
print("In use")
}))
UIApplication.shared.keyWindow?.rootViewController?.present(alertUsedEmail, animated: true, completion: nil)


default:
print("Create User Error: \(error!)")
}}

} else {
self.setUserInfo(user: user, pseudo : pseudo, password: password, data: data)


let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "nextView")

(UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil))!
print("user signed up successfully")

}
})
}

这是用于从 ViewController 文件调用注册函数的代码。

@IBAction func signUpAction(_ sender: Any) {

let data = UIImageJPEGRepresentation(self.UserImageView.image!, 0.8)

networkingService.signUp(email: emailTextField.text!, pseudo: pseudoTextField.text!, password: passwordTextField.text!, data: data! as NSData)

也许我不在正确的窗口中,但相同的代码在注册部分工作正常。

预先感谢您的帮助。

最佳答案

在 UIViewController 类型的函数 viewController 中添加一个新参数

func signUp(email: String, pseudo:String, password: String, data: NSData!,viewController: UIViewController){

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in

if error != nil {
print(error!.localizedDescription)


if let SignUperrCode = FIRAuthErrorCode(rawValue: error!._code) {
switch SignUperrCode {

case .errorCodeInvalidEmail:
let alertInvalidEmail = UIAlertController(title: "Email validation", message: "You have entered an malformed adress", preferredStyle: .alert)
alertInvalidEmail.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction) in
print("Invalid email")
}))

viewController.present(alertInvalidEmail, animated: true, completion: nil)


case .errorCodeEmailAlreadyInUse:
let alertUsedEmail = UIAlertController(title: "Email validation", message: "This email is already used", preferredStyle: .alert)
alertUsedEmail.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction) in
print("In use")
}))
viewController.present(alertUsedEmail, animated: true, completion: nil)


default:
print("Create User Error: \(error!)")
}}

} else {
self.setUserInfo(user: user, pseudo : pseudo, password: password, data: data)


let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "nextView")

(UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil))!
print("user signed up successfully")

}
})
}

并将其命名为

 networkingService.signUp(email: emailTextField.text!, pseudo: pseudoTextField.text!, password: passwordTextField.text!, data: data! as NSData, viewController: self)

关于ios - 尝试呈现 AlertController 时的窗口层次结构消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46264558/

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