0

如果用户回答 2 个或更多“是”,我应该问三个问题。该调查目前有 12 个问题,如果用户对两个或更多问题的回答是肯定的,那么我想添加 3 个额外的问题。(或者我总是将 3 个额外的问题添加到我的 ORKOrderedTask 中,然后我会跳过最后 3 个问题,因为用户没有对 2 个或更多问题回答“是”)。

我一直在玩“taskViewController didChange”,它可以让我检查当前问题的当前答案是“是”还是“否”。如果答案是“是”,我有一个计数器会不断增加。

现在,如果计数器达到 2,我只需要添加 3 个新问题。(或者如果计数器从未达到 2,则跳过最后 3 个问题。)

解决方案

无需修改当前调查,只需在第一次调查完成后立即创建新调查更容易。对我来说,我只需要检查用户回答“是”的次数。因此,我在“didFinish”(调查完成时执行的函数)中使用了这段代码来检查我的答案:

    //If reason.rawValue is 2,then survey was successfully completed.
    if(reason.rawValue == 2){
      if let results = taskViewController.result.results as? [ORKStepResult] {
          for stepResult: ORKStepResult in results {
              for result in ([ORKResult]?(stepResult.results!))!{
                  if let questionResult = result as? ORKQuestionResult {
                      print("\(questionResult.answer!)") // This print each answer
                  }
              }
          }
      }
   }

然后,你对答案做你需要做的事情,然后你打电话:

let viewController = self.storyboard?.instantiateViewController(withIdentifier: "IdentifierInYourStoryboard") taskViewController.present(viewController, animated: true, completion: nil)

或者制作一个转场或任何你想使用的过渡方法。我使用了故事板。

4

0 回答 0