我有一个swift文件,用于管理与Firebase的连接(注册,登录,保存数据)。在此文件中,使用一些AlertController根据来自Firebase的消息通知用户(错误的电子邮件格式,已使用的电子邮件)。问题是,当某些alerController必须显示时,它们会收到以下消息:
Warning: Attempt to present *** on *** whose view is not in the window hierarchy!
注册ViewController是当前视图,此视图链接到另一个特定文件。我已经在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)
也许我没有在正确的窗口,但相同的代码适用于注册部分。
提前感谢您的帮助。
答案 0 :(得分:1)
在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)