Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在视图顶部显示警报控制器,Swift_Ios_Swift_Swift3_Uialertcontroller - Fatal编程技术网

Ios 在视图顶部显示警报控制器,Swift

Ios 在视图顶部显示警报控制器,Swift,ios,swift,swift3,uialertcontroller,Ios,Swift,Swift3,Uialertcontroller,我有一个视图,它是keyWindow的子视图(如下面的代码所示)。我想在此视图顶部显示alertController(myView) 我试图在topViewController上显示alertController(如下所示),但由于我拥有的视图不是topViewController的一部分,因此此操作无效 guard let vc = UIApplication.topViewController() else { return} vc.present(alertController, anim

我有一个视图,它是keyWindow的子视图(如下面的代码所示)。我想在此视图顶部显示alertController(
myView

我试图在topViewController上显示alertController(如下所示),但由于我拥有的视图不是topViewController的一部分,因此此操作无效

guard let vc = UIApplication.topViewController() else { return}
vc.present(alertController, animated: true, completion: nil)
如何在视图顶部显示alertController

这就是我如何将视图添加到keyWindow的方法

guard let window = UIApplication.shared.keyWindow else{
    return
}

window.addSubview(myView)
由于该视图不是topViewController的一部分,因此以下操作不起作用

guard let vc = UIApplication.topViewController() else { return}
vc.present(alertController, animated: true, completion: nil)
试试这个

func showAlertMessage(title:String? = nil, _ message:String) {
    let rootVC = APP_DELEGATE.window?.topMostController()
    var alert = UIAlertController(title: message, message: nil, preferredStyle: .alert)
    if let title = title {
        alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    }
    alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
    rootVC?.present(alert, animated: true, completion: nil)
}试试这个

func showAlertMessage(title:String? = nil, _ message:String) {
    let rootVC = APP_DELEGATE.window?.topMostController()
    var alert = UIAlertController(title: message, message: nil, preferredStyle: .alert)
    if let title = title {
        alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    }
    alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
    rootVC?.present(alert, animated: true, completion: nil)

}

检查这个答案。这可以帮助你。您应该将其显示在当前关键点窗口的根视图控制器上,该控制器始终可见,并且始终位于顶部。@eneadume,它可以正常工作,谢谢..您可以查看此答案。这可以帮助你。您应该将其显示在当前关键点窗口的根视图控制器上,该控制器始终可见,并且始终位于顶部。@eneadume,它可以工作,谢谢。不客气