Is there a better way to pass around state in SwiftUI other than EnvironmentObjects?

112 views Asked by At

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?

0

There are 0 answers