0

我在使 UIAlerts 工作时遇到了一些麻烦。我查看了几个似乎可以解决此问题的 SO 问题,但我仍然遇到问题。似乎没有显示警报视图。这是我的代码:

        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)

这是错误:

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

有谁知道为什么没有提出这个?

非常感谢任何帮助或建议!

4

1 回答 1

0

这是因为当您调用呈现 UIAlertController 时,self.view 不在屏幕上。如果您在该viewDidLoad()部分中编写此代码,这将不起作用,因为 self.view 仍然不在屏幕上。

一旦 self.view 可用,您就可以进行此调用,例如在任何类型的 UI 操作中viewDidAppear()或从任何类型的 UI 操作(如单击按钮)中。基本上,之前会发生的任何事情viewDidDisappear()

如果您想阅读类似信息,还有一个类似的问题,这里还有一个更普遍的情况,即尝试呈现不在视图层次结构中的任何类型的视图控制器。

于 2015-07-27T18:13:25.627 回答