0

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.
LiohAu
  • 611
  • 12
  • 36

1 Answers1

0

Your view controller (the one containing the collection view) should contain the logic that mediates between your views and your data. The views should just report to the view controller what data there is and the view controller should do the validation (based on its access to the data).

The answer view can of course have its own view controller that takes care of the details displaying and laying out the UI elements and dealing with the user input. That makes sense because you have so many different scenarios. (If not, the collection view view controller could take care of it.)

Notify the view controller via your own delegate protocol. In UI element callbacks, such as textField:didEndEditing: and such, you can then call the delegate to inform it about the change in the data entered by the user.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I'm not sure to correctly understand this sentence "The answer view can of course have its own view controller that takes care of the details displaying and laying out the UI elements and dealing with the user input". Do you suggest me to create 1 VC for each answer ? If i follow this idea, i'll have to use view controller containment api in the collection view VC right ? – LiohAu Aug 24 '13 at 11:12