-1

I am working on a project using ResearchKit, so I send the user into the task I create:

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

When the user is finished with the survey, he goes into:

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

... }

Here I encounter a problem when I try to show an alert before

taskViewController.dismissViewControllerAnimated(true, completion: nil)

I get the below error:

Attempt to present UIAlertController: ... on ViewController: ... whose view is not in the window hierarchy

Any idea of how I could present the alert before dismissing the ViewController?

I use the below for the alert:

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)

EDIT: The code is as below:

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)

            }
asheyla
  • 3,175
  • 5
  • 18
  • 34
  • Could you dismiss the view controller on the click of the "OK" button in your alert controller? – j.f. Mar 28 '16 at 15:15
  • I do not want to dismiss yet, I first want to show on alert. The dismissing is working just fine. The problem is showing the alert. – asheyla Mar 28 '16 at 15:16
  • 1
    Yes, and @j.f. is saying to dismiss when tapping "OK" on the alert. – tktsubota Mar 28 '16 at 15:16
  • Right, you show the alert, and then when the user clicks the "OK" button on the alert, dismiss the view controller. – j.f. Mar 28 '16 at 15:17
  • I can't show the alert. Sorry for not explaining better. I get problems showing the alert. – asheyla Mar 28 '16 at 15:17
  • Yep, your problem is because you are trying to present the alert controller on a view controller that has now been dismissed, hence the "not in the window hierarchy" error. – j.f. Mar 28 '16 at 15:19
  • I have put an edit on the question to show how the code for the alert looks like and maybe for better explaining why I need to show the alert without dismissing. – asheyla Mar 28 '16 at 15:24
  • Is there additional, relevant code that we can't see? From the snippet you provided, it looks like either the view controller is dismissed, _or_ you present the alert controller - which should work. – j.f. Mar 28 '16 at 15:35
  • The presenting of the alert does not work. It gives the error in the title: Attempt to present on whose view is not in the window hierarchy – asheyla Mar 28 '16 at 15:38

3 Answers3

1

What object contains the httpResponse definition? The presentViewController() call looks like it's implicitly capturing self, on a view controller instance whose view perhaps is no longer in the view hierarchy when the response arrives.

jwe
  • 436
  • 2
  • 4
0

You can try this

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

for presenting the UIAlertController.

Waris Shams
  • 1,606
  • 1
  • 15
  • 27
0

You should call presentViewController on the taskViewController:ORKTaskViewController object

taskViewController.presentViewController(alertView, animated: true, completion: nil)
Stefan
  • 16
  • 1