【问题标题】:Attempt to present <UIAlertController: > on <ViewController:> whose view is not in the window hierarchy尝试在视图不在窗口层次结构中的 <ViewController:> 上呈现 <UIAlertController:>
【发布时间】:2016-03-28 15:07:47
【问题描述】:

我正在使用 ResearchKit 进行一个项目,因此我将用户发送到我创建的任务中:

let taskViewController = ORKTaskViewController(task: SurveyTask, taskRunUUID: nil)
taskViewController.delegate = self
presentViewController(taskViewController, animated: true, completion: nil)

当用户完成调查后,他进入:

func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
    switch reason {
        case .Completed:

... }

在这里我尝试在之前显示警报时遇到问题

taskViewController.dismissViewControllerAnimated(true, completion: nil)

我收到以下错误:

尝试在 ViewController: ... 上显示 UIAlertController: ... 其视图不在窗口层次结构中

知道如何在关闭 ViewController 之前显示警报吗?

我使用下面的警报:

let alertView = UIAlertController(title: "Houps", message: "Could not connect to the server.", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
presentViewController(alertView, animated: true, completion: nil)

编辑: 代码如下:

if let httpResponse = response as? NSHTTPURLResponse {
                print("HTTP response: \(httpResponse.statusCode)")
                if httpResponse.statusCode == 201 {
                    taskViewController.dismissViewControllerAnimated(true, completion: nil)
                }
            } else {
                print("No HTTP response")
                let alertView = UIAlertController(title: "Houps", message: "Could not connect to the server.", preferredStyle: .Alert)
                alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
                presentViewController(alertView, animated: true, completion: nil)

            }

【问题讨论】:

  • 您能否通过单击警报控制器中的“确定”按钮关闭视图控制器?
  • 我还不想解散,我首先想在戒备状态下显示。解雇工作得很好。问题是显示警报。
  • 是的,@j.f.在警报上点击“确定”时表示要关闭。
  • 对,你显示警报,然后当用户单击警报上的“确定”按钮时,关闭视图控制器。
  • 我无法显示警报。很抱歉没有更好地解释。我在显示警报时遇到问题。

标签: ios swift alert uialertaction researchkit


【解决方案1】:

什么对象包含 httpResponse 定义? presentViewController() 调用看起来像是在一个视图控制器实例上隐式捕获自我,当响应到达时,该实例的视图可能不再位于视图层次结构中。

【讨论】:

    【解决方案2】:

    你应该在 taskViewController:ORKTaskViewController 对象上调用 presentViewController

    taskViewController.presentViewController(alertView, animated: true, completion: nil)
    

    【讨论】:

      【解决方案3】:

      你可以试试这个

      UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(yourAlertController, animated: true, completion: nil)
      

      用于呈现 UIAlertController。

      【讨论】:

        猜你喜欢
        • 2019-12-25
        • 2023-03-31
        • 2016-01-06
        • 2018-06-06
        • 1970-01-01
        • 2017-04-01
        • 2016-11-29
        • 2015-09-17
        • 2014-02-02
        相关资源
        最近更新 更多