Alertcontroller Is Not in the Window Hierarchy

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

This line:

ViewController().present(alertView!, animated: true, completion: nil)

creates a new instance of ViewController and calls the present method on it. That won't work. You need to call it from a view controller that is itself presented. It looks like that code is inside ConsoleViewController, maybe you can just use self there.

AlertController is not in the window hierarchy

If you're instancing your UIAlertController from a modal controller, you need to do it in viewDidAppear, not in viewDidLoad or you'll get an error.

Here's my code (Swift 4):

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let alertController = UIAlertController(title: "Foo", message: "Bar", preferredStyle: .alert)

alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}

Attempt to present UIAlertController whose view is not in the window hierarchy (Swift 3/Xcode 8)

First create UIAlertController such an attribute.

var alertController: UIAlertController?

And you must add this in the viewDidLoad() like this:

override func viewDidLoad() {
super.viewDidLoad()

self.alertController = UIAlertController(title: "Alert", message: "Not images yet", preferredStyle: .alert)
self.alertController?.addAction(UIAlertAction(title: "Close", style: .default))
view.addSubview((alertController?.view)!)

}

So when you press signInButton and login is incorrect you must invoke.

@IBAction func signInPressed(_ sender: Any) {

if usernameTextField.text == "" || passwordTextField.text == "" {

createAlert(title: "Error in form", message: "Please enter an email and password.")

} else {

var activityIndicator = UIActivityIndicatorView()

activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
activityIndicator.center = self.view.center
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.shared.beginIgnoringInteractionEvents()

PFUser.logInWithUsername(inBackground: usernameTextField.text!, password: passwordTextField.text!, block: { (user, error) in

activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()

if error != nil {

self.presentedViewController?.present(self.alertController!, animated: true, completion: nil)
}
}

AlertController view is not in the window hierarchy

It seems you want to present the alert over the video player (playerLayer) but you are calling your alert over the layer behind the playerLayer, it will give you the error that you're getting.

With reference to Swift 4 Attempt to present ViewController whose view is not in the window hierarchy

Add this function in your code

func getTopMostViewController() -> UIViewController? {
var topMostViewController = UIApplication.shared.keyWindow?.rootViewController

while let presentedViewController = topMostViewController?.presentedViewController {
topMostViewController = presentedViewController
}

return topMostViewController
}

And replace (Line 33)

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

with

DispatchQueue.main.async {
getTopMostViewController()?.present(alert, animated: true, completion: nil)
}

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

try this

func currentTopViewController() -> UIViewController {
var topVC: UIViewController? = UIApplication.shared.delegate?.window?.rootViewController
while topVC?.presentedViewController {
topVC = topVC?.presentedViewController
}
return topVC!
}

and present the VC as

let currentTopVC: UIViewController? = self.currentTopViewController()

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

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

Use this to present Alert.

func showResponseAlert(title:String?,message:String?){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.present(alert, animated: true, completion: nil)
}
}


Related Topics



Leave a reply



Submit