【问题标题】:Warning: Attempt to present <UIAlertController: > > whose view is not in the window hierarchy警告:尝试呈现 <UIAlertController: > > 其视图不在窗口层次结构中
【发布时间】:2020-08-07 15:54:44
【问题描述】:

我正在尝试为我的应用创建和显示 UIalert。警报位于视图控制器内部,它将在 api 服务调用中被调用,如下所示。

    APIService().loginr(success: { result in
        print(result!)
        let json = result as! NSDictionary
        self.showResponseAlert(title: "Success!", message: json["message"] as? String)

    }, failure: {error in
        print(error!)
    }, parameters: parameters)

还有我的 uiAlert

func showResponseAlert(title:String?,message:String?){
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default))
    self.present(alert, animated: true, completion: nil)
}

我也试过了

   DispatchQueue.main.async{
       self.present(alertController, animated: true, completion: nil)
   }

但对我不起作用。

【问题讨论】:

  • 这能回答你的问题吗? AlertController is not in the window hierarchy
  • @Daniel 没有。生病用我的 UIAlert 更新问题
  • 您是否将 rootViewController 分配给项目中的任何 viewConrtoller?
  • @DinithePieris 您标记为答案的解决方案是我之前引用的问题中显示的答案之一。

标签: ios swift iphone xcode swift4


【解决方案1】:

使用它来显示警报。

func showResponseAlert(title:String?,message:String?){
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default))
    if var topController = UIApplication.shared.keyWindow?.rootViewController {
            while let presentedViewController = topController.presentedViewController {
                topController = presentedViewController
            }
            topController.present(alert, animated: true, completion: nil)
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2018-06-06
    • 2019-12-25
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多