2017-09-17 5 views
0

Firebase(サインアップ、サインイン、データ保存)との接続を管理するスワイプファイルがあります。このファイルでは、AlertControllerを使用して、Firebaseのメッセージ(間違った電子メール形式、すでに使用されている電子メール)に応じてユーザーに通知します。問題は、一部のalerControllerが現れなければならないとき、彼らはこのメッセージが表示されることにされていますAlertControllerを表示しようとするとウィンドウ階層のメッセージが表示される

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

を申し込みのViewControllerは、現在のビューであり、このビューは、別の特定のファイルにリンクされています。私はStackoverflowとGoogleの一般的な "ウィンドウ階層"で多くを読んで、さまざまなことを試してみました。

サインアップに使用した同様のコードで同じ問題が発生しました。

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

今では正常に動作しますが、それは私のサインアップのセクションで作業されていません。その一つに

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

:私はalertControllerを提示するために、この方法から行ってきました。 私はそれが現在の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) 

私は正しいウィンドウではないかもしれませんが、同じコードが申し込みセクションでうまく動作します。

ご協力いただきありがとうございます。

答えて

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) 
+0

として、それを呼び出してとてもサヒール、ありがとうございました。それは完全に動作します。 – Bastien

+0

@Bastienがうれしかった –

関連する問題