I am creating a quiz app that uses a ViewController named QuestionViewController to load and display the question data from a custom Question class. Right now, I am just checking the questionType of to determine how the Answer children of the Question need to be displayed. Then I have many if/then statements to display the answerTypes which can be:
- buttonType (radio/checkbox)
- inputFieldType
- datePickerType
- (any other types)
This works ok, but it’s really sloppy. I would like to load the Question then find the type of question using if/then in which I believe I should create custom view controller for each questionType and display that and maybe add protocols to return when the input has changed. Is this the right way to do this? I’d like to keep question types as dynamic as possible so I can add future types should we need them.
Am I on the right track? Is there something I’m missing/should improve on?
It sounds to me like you are on the right track. One thing to keep in mind though is that your questions will ALL likely have to do a few things the same, such as… display the question! This lends itself well to subclassing as follows:
UIViewController->QuestionViewController->CustomQuestionViewController1UIViewController->QuestionViewController->CustomQuestionViewController2UIViewController->QuestionViewController->CustomQuestionViewController3etc.
Every
Questionwill have to have certain information to display regardless of the question/answer type. For example, it is likely that every one of your questions will have some sort ofUILabelorUITextViewdisplaying the text for the question. This label’s position, font, etc. will probably not change from question to question, so it should be setup in the question’sQuestionViewController, where as the answers (whose format will likely vary, as you mention) can be setup in the variousCustomQuestionViewControllers that you implement. This will greatly reduce potential code duplication in your question’s view controllers because the things they do the same can be done inside yourQuestionViewController.If you did your subclassing like below…
UIViewController->CustomQuestionViewController1UIViewController->CustomQuestionViewController2You would likely end up with a lot of duplicate code setting up the basic question display.
Also, name them better than
CustomQuestionViewController1haha.