ios - How to validate values of UICollectionViewCell from the UICollectionViewController -
i have following design problem, , hope me solve :)
i have view controller takes questionnaire model instance , render questionnaire using collectionview. questionnaire model contains array of questions models (each question instance different subclass of base question class depending of answer type, example : date question, boolean question...).
each cell in collectionview represent question , contain answer views loaded .xib depending on answer type (1 xib = 1 type of answer). answer view may contain uiswitch, uitextfield or other control allow user answer question.
at moment answer view configure control depending on answer validation rules (for example date question may have datepicker minimumdate/maximumdate properties configured depending on in date question model, or correct keyboard textfields etc...). answer view delegate/observer/target of control, , when value change, view set value (using formatter if needed , depending on answer type) in destination object. think have problem here, have view setting value in model without using controller, not mvc right ?
now main problem: view controller should able validate values, , depending on validation result, display error label on top of screen, , unhide error icon. how can viewcontroller notified of value change ? , need retrieve associated answer view instance display error icon (without reloading question cell avoid "blink" effect , loss of control focus) ?
my 1st idea set controller instance question cell, , question cell pass instance answer subview(s), answer view call methods on controller when answer value change. suppose having view containing controller instance bad pattern right ?
the 2nd idea: using kvo on properties of object, , when value change, retrieve associated question model using modified value keypath, can apply validation rules controller, how can retrieve question view instance change error icon visibility ?
i don't have 3rd idea, , why wrote long post :)
little recall of involved classes : questionnaire model instance contains subclasses of question model instances, questionnaire view controller display "question" cells each "question" model instance. "question" cell contains view each answer.
ps : purposely used .xib contain answer, , not question , answer because have other constraints, :
- having multiple question layout not based on answer type, , did not wanted have (number of answers type * number of question layouts) .xib
- having multiple answers 1 question.
your view controller (the 1 containing collection view) should contain logic mediates between views , data. views should report view controller data there , view controller should validation (based on access data).
the answer view can of course have own view controller takes care of details displaying , laying out ui elements , dealing user input. makes sense because have many different scenarios. (if not, collection view view controller take care of it.)
notify view controller via own delegate protocol. in ui element callbacks, such textfield:didendediting:
, such, can call delegate inform change in data entered user.
Comments
Post a Comment