我正在使用 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)
}