Reyansh Jr.
Reyansh Jr.

Reputation: 33

Attempt to present UIAlertController whose view is not in the window hierarchy

more specifically the error warning I ma getting is:

Attempt to present UIAlertController whose view is not in the window hierarchy!

I am currently creating a signup page that ask user to fill few specific details such as Name, Country, Email, Password and etc. To make sure user provide all the relevant information I am trying to write a code to send an alert if user does not provide all the information. I have wrote the code taking help from stakeoverflow.

Problem: Whenever user left any field blank it is not showing the alert and by default performing a segue that takes user to signin page. This is the first time I am creating an alert and hence don't what is going wrong (I believe 95% of my code is in place)

can anyone help?

     @IBAction func signUpPressed(_ sender: Any) {

    if nameText.text!.isEmpty || genderText.text!.isEmpty || countryText.text!.isEmpty || yourSchool.text!.isEmpty || yourClass.text!.isEmpty {

        print("Please fill all fields") 

        //my code is printing above error in the Xcode console but the below code is not working 

        //setting error message if not all fiels filled
        let alertController = UIAlertController(title: "Error", message: "Please fill all fields", preferredStyle: .alert)

        let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertController.addAction(defaultAction)

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

    }

    else {

        Auth.auth().createUser(withEmail: yourEmail.text!, password: yourPassword.text!) { (user, error) in

            if error != nil {

                ///print errror message
                let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)

                self.present(alertController, animated: true, completion: nil)

            } else {

                print("You have successfully signed up")

                self.performSegue(withIdentifier: "JoinUs2SignPage", sender: self)

                //updating user information
                let userID = Auth.auth().currentUser!.uid
                let usertype: String = "Student"
                self.ref.child("users").child(userID).setValue(["usertype": usertype ,"username": self.nameText.text!, "usergender": self.genderText.text!, "usercountry": self.countryText.text!, "userschool": self.yourSchool.text!, "userclass": self.yourClass.text!,])
            }
        }
    }

Upvotes: 1

Views: 1297

Answers (2)

Reyansh Jr.
Reyansh Jr.

Reputation: 33

@IBAction func registerPressed(_ sender: Any) {

    if nameText.text!.isEmpty || genderText.text!.isEmpty || countryText.text!.isEmpty || yourSchool.text!.isEmpty || yourClass.text!.isEmpty {

        print("Please fill all fields") //my code is printing this error

        //alert message popup

        let alertController = UIAlertController(title: "Error", message: "Please fill all fields", preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in
        print("Okay")
        }))

        let alertWindow = UIWindow(frame: UIScreen.main.bounds)
        alertWindow.rootViewController = UIViewController()
        alertWindow.windowLevel = UIWindowLevelAlert
        alertWindow.makeKeyAndVisible()
        alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)


    }

    else {

        Auth.auth().createUser(withEmail: yourEmail.text!, password: yourPassword.text!) { (user, error) in

                    if error != nil {

                        ///print errror message

                        let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
                        alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in
                            print("Okay")
                        }))

                        let alertWindow = UIWindow(frame: UIScreen.main.bounds)
                        alertWindow.rootViewController = UIViewController()
                        alertWindow.windowLevel = UIWindowLevelAlert + 1;
                        alertWindow.makeKeyAndVisible()
                        alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)

                    }

                    else {

                        print("You have successfully signed up")

                        self.performSegue(withIdentifier: "JoinUs2SignPage", sender: self)

                        //updating user information
                        let userID = Auth.auth().currentUser!.uid
                        let usertype: String = "Student"
                        self.ref.child("users").child(userID).setValue(["usertype": usertype ,"username": self.nameText.text!, "usergender": self.genderText.text!, "usercountry": self.countryText.text!, "userschool": self.yourSchool.text!, "userclass": self.yourClass.text!,])
                        }
            }
        }

}

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

Action may be out of main thread

  DispatchQueue.main.async {

  ///print errror message
   let alertController = UIAlertController(title: "Error",    message: error?.localizedDescription, preferredStyle: .alert)

   let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
   alertController.addAction(defaultAction)

  self.present(alertController, animated: true, completion: nil)


}

Try this approach

check for white space like that

 let nameTrimmed = self.nameText.text?.trimmingCharacters(in: .whitespacesAndNewlines)

 let genderTrimmed = self.genderText.text?.trimmingCharacters(in: .whitespacesAndNewlines)

 let countryTrimmed = self.countryText.text?.trimmingCharacters(in: .whitespacesAndNewlines)

 let schoolTrimmed = self.schoolText.text?.trimmingCharacters(in: .whitespacesAndNewlines)



 if  nameTrimmed.text == "" ||  genderTrimmed.text == "" ||   countryTrimmed.text == "" ||  schoolTrimmed.text == "" 

Upvotes: 0

Related Questions