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

标签 ios swift xcode uialertcontroller

我有一个人们可以用来请求服务的应用程序。我有一个选项卡栏 Controller ,它在第一个选项卡上使用导航 Controller ,该选项卡有多个页面用于不同的内容,例如服务、位置和客户信息。所有这些都有 UIAlertViews,当我将 tabBarController 作为应用程序入口点时,一切都运行良好。然后我决定添加一个不同的 UIViewController,他们可以选择 tabBarController 或不同的 tabBarController。如果有意义的话,有点像 2 个应用程序合二为一。当我将该 UIViewController 作为应用程序入口点时,除最后一个之外的所有 UIAlertView 都可以工作。它说“警告:尝试呈现不在窗口层次结构中的 View !”。这是我的代码

@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

当我将应用程序入口点放在标签栏 Controller 上时(上图),一切都很好。但是当我将入口点设置为 UIView(下图)时,只有最后一部分不起作用。

最佳答案

使用这段代码:

    let yourCurrentVisibleViewController = UIApplication.shared.keyWindow!.rootViewController

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

或者将您的 Controller 设置为上面yourCurrentVisibleViewController的子 Controller 。也许您尝试在 Controller 未出现之前调用方法。

关于ios - UIAlertController 不在不同入口点的层次结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54992714/

相关文章:

ios - 如何从后台调用locationManager didUpdateToLocation?

swift - iPad Swift Playgrounds 无法运行 ARKit 代码/崩溃

ios - 如何在 Xcode 中重新切片图像?

objective-c - 如何将 ABPeoplePickerNavigationController 设置为 MainView?

ios - 从 View 中删除 UIView

iphone - 设置 UIDatePicker 日期时崩溃

ios - 在 iOS 8 中录制音频是否需要请求许可?

ios - 关于在 Swift 的静态 tableviewController 中放置固定按钮的问题

ios - 根据对象类型更改 iOS 上的注释图像

iOS:Tabbar - 加载选项卡的默认状态