开发者

Super/sub-classes and views in an iPhone app

I have an iPhone app that presents a questionnaire. The questionnaire consists of several questions.

I have created a QuestionnaireViewController class which loads up several QuestionViewController objects based on the number of questions in that questionnaire (this is pulled in from an external data source).

This works fine.

The app is now going through a further iteration and the requirements are slightly more complex. The above prototype loads in questions with the same question view (i.e. one type of question). However several types of question exist (slider-based, text input etc).

All types of question share a subset of properties:

A questions has a title, id, sectionId and question number

The QuestionViews will differ in the way that data is input. Some require data to be input using a slider, whereas some require text input.

This immediately screams a superclass/subclass structure:

Question
  SliderQuestions
  TextInputQuestions
  etc

My questions are:

1) How can I load a specific subclass of a view based on some data value?

e.g.

if (开发者_如何学Python[questionType isEqualToString:@"slider"]) {
  //load slider view
} else if ([questionType isEqualToString:@"textInput"]) {
  //load text input view
}

Do I have to build a View AND ViewController for each class? How can I model this superclass subclass structure?

My confusion lies when it comes to creating views and view controllers. Obviously I need a new view to handle the question specific components such as adding a slider to one and a text input to another, but how do I show the shared data (superclass properties) without having to duplicate for each view?


I recommend that you consider using tags to identify UIViews if you have many of them. Otherwise, you can compare strings with

[questionType isEqualToString:@"string"];

Though the latter approach is more description, using tags will allow you to set up a switch, which makes the code a bit more logical and more compact (IMHO).


The answer to your question is: yes, you can have separate views for different question types. If the data from the answers are similar I would use a single view controller with switches to a) load alternate views and b) process different types of response (if necessary).

So, single view controller, separate views would be simplest, but you could just as well use a single view and show/hide different controls Programatically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜