0

I have been having some trouble making UIAlerts work. I have looked at a couple SO questions that seem to solve this issue yet I still have a problem. The alert view seems to not be presented. Here is my code:

        var inputTextField: UITextField?

        let actionSheetController: UIAlertController = UIAlertController(title: "Create a password", message: "", preferredStyle: .Alert)

        let save: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
            if !(inputTextField?.text=="password"){
                println(inputTextField?.text)
            }else{
                println("You have a really bad password")
            }
        }
        actionSheetController.addAction(save)
        actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
            inputTextField = textField
        }
        self.presentViewController(actionSheetController, animated: true, completion: nil)

Here is the error:

Attempt to present <UIAlertController: 0x7fa7016305e0> on <PassProtect.ViewController: 0x7fa701576600> whose view is not in the window hierarchy!

Does anybody know why this is not being presented?

Any help or advice is greatly appreciated!

Daniel
  • 3,188
  • 14
  • 34
  • It's complaining that PassProtect.ViewController's view is not currently being shown and therefore it doesn't have a context for presenting the next controller's view. How do you create the view controller that has the code you posted? How do you display its view? – Phillip Mills Jul 27 '15 at 17:03
  • Your code works for me - calling it as a function from a test button. – simons Jul 27 '15 at 17:06
  • 1
    [Similar question](http://stackoverflow.com/questions/27197677/error-when-presenting-uialertcontroller). It's to do with the fact that when you make the call to present the UIAlertController, self.view is not on screen. If you are writing this code in the viewDidLoad() section, this won't work as self.view is still off screen. You can make this call once self.view is available, for example in viewDidAppear(). – Matt Le Fleur Jul 27 '15 at 17:13
  • possible duplicate of [whose view is not in the window hierarchy](http://stackoverflow.com/questions/11862883/whose-view-is-not-in-the-window-hierarchy) – Angela Jul 27 '15 at 17:25
  • where did you call this function into **ViewDidLoad or ViewwillAppear** – Lamour Jul 27 '15 at 17:28
  • I call it in viewDidLoad() – Daniel Jul 27 '15 at 17:47
  • @Phoen1xUK I suggest that you please repost your comment as an answer, for you fixed my issue. Thanks :) – Daniel Jul 27 '15 at 17:49

1 Answers1

0

It's to do with the fact that when you make the call to present the UIAlertController, self.view is not on screen. If you are writing this code in the viewDidLoad() section, this won't work as self.view is still off screen.

You can make this call once self.view is available, for example in viewDidAppear() or from any sort of UI action like clicking a button. Basically, anything that will occur before viewDidDisappear().

There is a similar question with similar information if you want to read it and also here for a more generalised case, i.e. trying to present any sort of view controller which isn't in the view hierarchy.

Community
  • 1
  • 1
Matt Le Fleur
  • 2,708
  • 29
  • 40