我知道这个问题被问了很多次,我读了答案,但仍然无法解决我的问题。 所以,这里是 class SurveyTask 。
public class SurveyTask: NSObject, ORKTask {
let introStepID = "intro_step"
let nameStepID = "name_step"
let questStepID = "quest_step"
let colorStepID = "color_step"
let summaryStepID = "summary_step"
public var identifier: String { get { return "survey"} }
public func stepBeforeStep(step: ORKStep?, withResult result: ORKTaskResult) -> ORKStep? {
switch step?.identifier {
case .Some(nameStepID):
return stepWithIdentifier(introStepID)
case .Some(questStepID):
return stepWithIdentifier(nameStepID)
case .Some(summaryStepID):
return questStep(findName(result))
default:
return nil
}
}
public func stepAfterStep(step: ORKStep?, withResult result: ORKTaskResult) -> ORKStep? {
switch step?.identifier {
case .None:
return stepWithIdentifier(introStepID)
case .Some(introStepID):
return stepWithIdentifier(nameStepID)
case .Some(nameStepID):
return questStep(findName(result))
case .Some(questStepID):
return stepWithIdentifier(summaryStepID)
default:
return nil
}
}
public func stepWithIdentifier(identifier: String) -> ORKStep? {
switch identifier {
case introStepID:
let instructionStep = ORKInstructionStep(identifier: introStepID)
instructionStep.title = "The Questions Three"
instructionStep.text = "Who would cross the Bridge of Death must answer me these questions three, ere the other side they see."
return instructionStep
case nameStepID:
let nameAnswerFormat = ORKTextAnswerFormat(maximumLength: 20)
nameAnswerFormat.multipleLines = false
let nameQuestionStepTitle = "What is your name?"
return ORKQuestionStep(identifier: nameStepID, title: nameQuestionStepTitle, answer: nameAnswerFormat)
case questStepID:
return questStep("")
case summaryStepID:
let summaryStep = ORKCompletionStep(identifier: "SummaryStep")
summaryStep.title = "Right. Off you go!"
summaryStep.text = "That was easy!"
return summaryStep
default:
return nil
}
}
func findName(result: ORKTaskResult) -> String? {
if let stepResult = result.resultForIdentifier(nameStepID) as? ORKStepResult, let subResults = stepResult.results, let textQuestionResult = subResults[0] as? ORKTextQuestionResult {
return textQuestionResult.textAnswer
}
return nil
}
func questStep(name: String?) -> ORKStep {
var questQuestionStepTitle = "What is your quest?"
if let name = name {
questQuestionStepTitle = "What is your quest, \(name)?"
}
let textChoices = [
ORKTextChoice(text: "Create a ResearchKit App", value: 0),
ORKTextChoice(text: "Seek the Holy Grail", value: 1),
ORKTextChoice(text: "Find a shrubbery", value: 2)
]
let questAnswerFormat: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormatWithStyle(.SingleChoice, textChoices: textChoices)
return ORKQuestionStep(identifier: questStepID, title: questQuestionStepTitle, answer: questAnswerFormat)
}}
在 viewController :
中override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
guard let activity = Activity(rawValue: indexPath.row) else { return }
let taskViewController: ORKTaskViewController
switch activity {
case .Survey:
taskViewController = ORKTaskViewController(task: SurveyTask(), taskRunUUID: nil)
}
taskViewController.delegate = self
navigationController?.presentViewController(taskViewController, animated: true, completion: nil)
}
当我构建并运行我的项目并推动Survey它开始(!)并且我看到调查的第一步,但是当我点击“开始”它在 findName 功能中停止了条件(我添加了一张图片,以便更好地显示它停在哪里http://upload.akusherstvo.ru/image1020309.png):
func findName(result: ORKTaskResult) -> String? {
if let stepResult = result.resultForIdentifier(nameStepID) as? ORKStepResult, let subResults = stepResult.results, let textQuestionResult = subResults[0] as? ORKTextQuestionResult {//////here
return textQuestionResult.textAnswer
}
return nil
}
你是对的,@ conarch,一个我想要访问的数组真的是空的。但我无法理解为什么它是空的。我想做一个“依赖”调查(前一个问题的答案决定下一个问题是什么)。为此,我需要得到问题的结果。