0

目前我正在使用研究工具包构建一个应用程序。下面是我的问题。

let onsetQuestionStepTitle = "Title"
let onsetQuestionStepQuestion = "Question Name"
let onsetTextChoices = [
    ORKTextChoice(text: "superb", value: 0 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "great", value: 1 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "fair", value: 2 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "not good", value: 3 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "bad", value: 4 as NSCoding & NSCopying & NSObjectProtocol)
]
let onsetAnswerFormat: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: onsetTextChoices)
let onsetQuestionStep = ORKQuestionStep(identifier: "twoAwakeSurvey", title: onsetQuestionStepTitle, question: onsetQuestionStepQuestion, answer: onsetAnswerFormat)
steps += [onsetQuestionStep]

如果用户点击精湛,我想跳到带有标识符“threeAwakeSurvey”的下一步并转到“fourAwakeSurvey”。为此,我实现了以下代码。

var task = ORKNavigableOrderedTask(identifier: "StepNavigationTaskIdentifier", steps: steps)
let resultSelector: ORKResultSelector = ORKResultSelector(resultIdentifier: "twoAwakeSurvey")
let askLocation = ORKResultPredicate.predicateForChoiceQuestionResult(with: resultSelector, expectedAnswerValue: 0 as NSCoding & NSCopying & NSObjectProtocol)

let locationNavigationRule = ORKPredicateSkipStepNavigationRule(resultPredicate: askLocation)
task.setSkip(locationNavigationRule, forStepIdentifier: "threeAwakeSurvey")

但是,这是行不通的。我阅读了文档,但仍然不明白为什么。

4

1 回答 1

1

我在操场上尝试了下面的代码及其工作。也许您的第 3 步标识符不正确。

let onsetQuestionStepTitle = "Title"
let onsetQuestionStepQuestion = "Question Name"
let onsetTextChoices = [
    ORKTextChoice(text: "superb", value: 0 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "great", value: 1 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "fair", value: 2 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "not good", value: 3 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "bad", value: 4 as NSCoding & NSCopying & NSObjectProtocol)
]
let onsetAnswerFormat: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: onsetTextChoices)
let onsetQuestionStep = ORKQuestionStep(identifier: "twoAwakeSurvey", title: onsetQuestionStepTitle, question: onsetQuestionStepQuestion, answer: onsetAnswerFormat)

let threeAwakeSurveyStep = ORKInstructionStep(identifier: "threeAwakeSurvey")
threeAwakeSurveyStep.title = "Three"

let fourAwakeSurveyStep = ORKInstructionStep(identifier: "fourAwakeSurvey")
fourAwakeSurveyStep.title = "Four"

let steps = [onsetQuestionStep, threeAwakeSurveyStep, fourAwakeSurveyStep]

var task = ORKNavigableOrderedTask(identifier: "StepNavigationTaskIdentifier", steps: steps)
let resultSelector: ORKResultSelector = ORKResultSelector(resultIdentifier: "twoAwakeSurvey")
let askLocation = ORKResultPredicate.predicateForChoiceQuestionResult(with: resultSelector, expectedAnswerValue: 0 as NSCoding & NSCopying & NSObjectProtocol)

let locationNavigationRule = ORKPredicateSkipStepNavigationRule(resultPredicate: askLocation)
task.setSkip(locationNavigationRule, forStepIdentifier: "threeAwakeSurvey")

let taskVC = ORKTaskViewController(task: task, taskRun: nil)

PlaygroundPage.current.liveView = taskVC
于 2018-07-01T03:22:28.073 回答