この質問が何度も出されていることは知っており、回答を読みましたが、それでも問題を解決できません。したがって、ここにクラス 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 を押すと、開始され (!)、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、私がアクセスしようとしている配列は本当に空です。しかし、なぜそれが空なのか理解できません。「依存」調査を行いたいです (前の質問への回答によって、次の質問が決まります)。そのためには、質問の結果を取得する必要があります。