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

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]


如果用户点击“ superb”,我想跳到带有标识符“ 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")


但是,这不起作用。我阅读了文档,但仍然不明白为什么。

最佳答案

我在操场上及其工作中尝试了以下代码。也许您的第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

关于swift - 无法使用研究工具包跳过步骤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51107477/

10-12 14:32