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
How to Enable Back/Left Swipe Gesture in Uinavigationcontroller After Setting Leftbarbuttonitem
Identify New iPhone Model on Xcode (5, 5C, 5S)
How to Reload Tableview from Another View Controller in Swift
Getting Device Orientation in Swift
Set Dimensions for Uiimagepickercontroller "Move and Scale" Cropbox
Saving Image to Documents Directory and Retrieving for Email Attachment
Implement Pushkit and Test in Development Behavior
How to Put an Image in a Realm Database
Add Custom Header Field in Request of Avplayer
How to List Out All the Subviews in a Uiviewcontroller in iOS
Customize Mkannotation Callout View
iOS Swiftui: Pop or Dismiss View Programmatically
Jenkins - Xcode Build Works Codesign Fails
"Do Not Embed", "Embed & Sign", "Embed Without Signing". What Are They. What They Do