【问题标题】:Can't skip a step using research kit使用研究工具包不能跳过一个步骤
【发布时间】:2018-06-29 18:24:41
【问题描述】:

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

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")

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

【问题讨论】:

    标签: swift researchkit


    【解决方案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
    

    【讨论】:

    • 您好,感谢您的评论我应该在哪里使用 ORKTaskViewController(task: task, taskRun: nil)?我没有在 ORKTaskViewControllerDelegate 中做任何事情。
    • 您可以在应用程序中的任何 UIViewController 上呈现 ORKTaskViewController。例如,@IBAction func openTask(sender: UIButton) { viewController.present(taskVC, animated: true) }
    猜你喜欢
    • 2015-06-19
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多