I have the following design problem, and i hope that anybody will help me to solve it :)
I have a view controller that takes a questionnaire model instance and render this questionnaire using a collectionview. This questionnaire model contains an array of questions models (each question instance is a different subclass of the base question class depending of the answer type, for example : date question, boolean question...).
Each cell in the collectionview represent a question and contain answer views that are loaded from a .xib depending on the answer type (1 xib = 1 type of answer). An answer view may contain a UISwitch, a UITextField or any other control that allow the user to answer the question.
At the moment the answer view configure the control depending on the answer validation rules (for example a date question may have the datepicker minimumDate/maximumDate properties configured depending on what is in the date question model, or a correct keyboard for textfields etc...). The answer view is also the delegate/observer/target of the control, and when its value change, the view set the value (using a formatter if needed and depending on the answer type) in the destination object. I think that i already have a problem here, i have a View setting a value in the Model without using the Controller, not really MVC right ?
Now my main problem: the View Controller should be able to validate the values, and depending on the validation result, display an error label on the top of the screen, and unhide an error icon. But how can i get my viewcontroller notified of the value change ? and i also need to retrieve the associated answer view instance to display the error icon (without reloading the question cell to avoid a "blink" effect and loss of control focus) ?
My 1st idea was to set the controller instance to the question cell, and then the question cell could pass this instance to its answer subview(s), then the answer view could call methods on the controller when the answer value change. But i suppose that having a view containing the controller instance is a bad pattern right ?
The 2nd idea: using KVO on all the properties of the object, and when the value change, retrieve the associated question model using the modified value keypath, so i can apply the validation rules from the controller, but how can i retrieve the question view instance to change the error icon visibility now ?
I don't have a 3rd idea, and this is why i wrote this so long post :)
Little recall of involved classes : Questionnaire model instance contains subclasses of Question model instances, Questionnaire View Controller display "Question" cells for each "Question" model instance. "Question" cell contains a view for each answer.
PS : I purposely used the .xib to contain only the answer, and not the question and its answer because i have other constraints, like :
- having multiple question layout that are not based on the answer type, and i did not wanted to have (number of answers type * number of question layouts) .xib
- having multiple answers for one question.