2017-09-17 113 views
0

我有一個用於管理與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) 

也許我不是在右邊的窗口,但相同的代碼工作正常的註冊部分。

在此先感謝您的幫助。

回答

1

添加一個新的參數類型的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) 
+0

非常感謝您薩赫勒。它完美的作品。 – Bastien

+0

@Bastien很高興幫助 –

相關問題