0

I'm fairly new to SwiftUI. I'm creating a survey view. In it, I load data from a Plist to show a list of multiple choice questions. I've created the following:

SurveyView (holds the whole survey) SurveyQuestionView (holds a single multiple choice question) SurveyAnswerView (holds an answer to a single potential answer, and is basically a customized button)

I'm able to pass in all of the data in a great way that allows me to pass the data from the parent to the child. The issue that I'm facing is finding a good way to pass the data back up.

Specifically, when a user taps on a question, it's selected, and all other answers must become unselected. The parent needs to be notified about which answer was tapped, but the answer does not know what answer it is.

Furthermore, when the user taps complete, I need to be able to know what each selected answer is.

One possible solution is to create an environment object with all of the answers, but then each answer at the bottom must know not only what answer index it is, but also the question index. This seems like a lot more than I would like this simple button to contain.

Is there a better way?

pawello2222
  • 46,897
  • 22
  • 145
  • 209
Matthew Knippen
  • 1,048
  • 9
  • 22
  • 1
    It looks like in your case you can use `@Binding` for two way communication between the question view and its parent. But please add some code examples to your question. – pawello2222 Dec 15 '20 at 21:32
  • It sounds like a database thing. Try CoreData and you can export surveys so you can see them outside of the app. CoreData objects are ObservableObjects so they can be passed around just the same with an EnvironmentObject and they would survive interruptions as required by Apple. – lorem ipsum Dec 15 '20 at 21:34
  • @loremipsum This is a very small 3 question survey. I really don't need an entire DB for it. – Matthew Knippen Dec 15 '20 at 21:57
  • @Binding will do the job if you want a clear answer post the code – Cod3rMax Dec 15 '20 at 22:14

0 Answers0