UIAlertController不在层次结构中的不同入口点

时间:2019-03-04 22:40:50

标签: ios swift xcode uialertcontroller

我有一个可供人们请求服务的应用程序。我有一个选项卡栏控制器,该控件在第一个选项卡上使用导航控制器,该控件具有多个页面,用于显示不同的内容,例如服务,位置和客户信息。它们都具有UIAlertViews,当我将tabBarController用作应用程序的入口点时,一切工作都很好。然后,我决定添加一个其他的UIViewController,他们可以选择tabBarController或另一个tabBarController。如果有意义的话,可以将2种应用合而为一。当我将该UIViewController用作应用程序入口点时,除最后一个之外,所有UIAlertViews都可以工作。它说:“警告:尝试显示不在窗口层次结构中的视图!”。这是我的代码

@IBAction func send(_ sender: Any)
{

    name.resignFirstResponder()
    dismiss(animated: true, completion: nil)
    year.resignFirstResponder()
    dismiss(animated: true, completion: nil)
    make.resignFirstResponder()
    dismiss(animated: true, completion: nil)
    model.resignFirstResponder()
    dismiss(animated: true, completion: nil)
    color.resignFirstResponder()
    dismiss(animated: true, completion: nil)
    note.resignFirstResponder()
    dismiss(animated: true, completion: nil)

    let alertController = UIAlertController(title: "You will now email your service request. The recipient email adress and message will be preset, all you have to do is hit send.", message: "Message and data rates may apply.", preferredStyle: .alert)
    let okAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.default)
    {
        UIAlertAction in
        let mailcomposedViewController = self.configureMailController()
        if MFMailComposeViewController.canSendMail()
        {
            self.present(mailcomposedViewController, animated: true, completion: nil)
        }
        else
        {
            self.showMailError()
        }
    }

    alertController.addAction(okAction)


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

有人能为我指出警报在一个入口点而不是另一个入口起作用的正确方向吗?

enter image description here

enter image description here

当我将应用程序入口点放在选项卡栏控制器上(上图)时,一切正常。但是,当我将入口点设置为UIView(底部图片)时,只有最后一部分不起作用。

2 个答案:

答案 0 :(得分:0)

使用此代码:

    let yourCurrentVisibleViewController = UIApplication.shared.keyWindow!.rootViewController

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

或将您的控制器设为上述 yourCurrentVisibleViewController 的子控制器。也许您尝试在未显示控制器之前调用方法。

答案 1 :(得分:0)

问题是这一行:

dismiss(animated: true, completion: nil)

您一遍又一遍地说(无缘无故)。您(self)是显示的视图控制器,因此您现在要关闭您自己。因此,当您说self.present时,您就不再处于视图层次结构中。