尝试呈现AlertController时的窗口层次结构消息
问题描述:
我有一个用于管理与Firebase(注册,登录,保存数据)连接的swift文件。在此文件中,使用一些AlertController通知用户取决于来自Firebase的邮件(错误的电子邮件格式,已使用的电子邮件)。问题是,当一些alerController有露面,他们收到此消息:尝试呈现AlertController时的窗口层次结构消息
Warning: Attempt to present *** on *** whose view is not in the window hierarchy!
一个注册的ViewController是当前视图,该视图链接到另一个特定的文件。我已经在Stackoverflow和Google上的“windows层次结构”中读了很多,并尝试了不同的东西。
我和用于注册的类似代码有相同的问题。我从这种方式去呈现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")
}
})
}
这里是代码使用调用从视图控制器文件中的注册功能。
@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
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")
}
})
}
您的viewController功能并把它作为
networkingService.signUp(email: emailTextField.text!, pseudo: pseudoTextField.text!, password: passwordTextField.text!, data: data! as NSData, viewController: self)
非常感谢您萨赫勒。它完美的作品。 – Bastien
@Bastien很高兴帮助 –