2016-03-28 2 views
-1

Я работаю над проектом с помощью ResearchKit, поэтому я посылаю пользователя в задачу, которую я создаю:Попытка представить <UIAlertController:> на <ViewController:> чья точка зрения не в иерархии окон

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) 

Я получаю ошибку ниже:

Попытка представить UIAlertController: ... на ViewController: ... чье мнение не в окне иерархии

Любая идея, как я может представить предупреждение перед тем, как отклонить 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) 

EDIT: код, как показано ниже:

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) 

      } 
+0

Не могли бы вы закрыть контроллер представления о нажатием кнопки «OK» в вашем оповещения контроллера? –

+0

Я не хочу увольнять, я сначала хочу показать вслух. Увольнение работает отлично. Проблема показывает предупреждение. – asheyla

+1

Да, и @ j.f. говорит, что увольняется, когда нажимает «ОК» на предупреждение. – tktsubota

ответ

0

Вы должны позвонить presentViewController на taskViewController: ORKTaskViewController объект

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

Да, вот и все! Оно работает! Спасибо, Стефан! – asheyla

0

Вы можете попробовать это

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

для представления UIAlertController.

1

Какой объект содержит определение httpResponse? Вызов presentViewController() выглядит так, будто он неявно захватывает «я», на экземпляре диспетчера представлений, чей вид, возможно, больше не находится в иерархии представлений, когда приходит ответ.

Смежные вопросы