【发布时间】:2018-02-26 02:54:14
【问题描述】:
我有一个 swift 文件用于管理与 Firebase 的连接(注册、登录、保存数据)。在此文件中,使用一些 AlertController 根据来自 Firebase 的消息通知用户(错误的电子邮件格式,已使用电子邮件)。问题是当一些alerController必须出现时,他们会收到这个消息:
Warning: Attempt to present *** on *** whose view is not in the window hierarchy!
Signup 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")
}
})
}
这是用于从 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)
也许我不在正确的窗口中,但相同的代码在注册部分可以正常工作。
提前感谢您的帮助。
【问题讨论】:
标签: ios swift3 xcode8 uialertcontroller