iPhone subview design (UIView vs UIViewController)

12,928

You're on the right track. In your QuizController xib, you can create separate views by dragging them to the xib's main window rather than to the QuizController's main view. Then you can design each view you need according to your question types. When the user taps next or previous, remove the previous view and load the view you need based on your question type using -addSubview on the view controller's main view and keep track of which subview is currently showing. Trying something like this:

[currentView removeFromSuperView];

switch(questionType)
{
    case kMultipleChoice:
        [[self view] addSubview:multipleChoiceView];
        currentView = multipleChoiceView;
        break;
    case kOpenEnded:
        [[self view] addSubview:openEndedView];
        currentView = openEndedView;
        break;
// etc.
}

Where multipleChoice view and openEndedView are UIView outlets in your QuizController connected to the views you designed in IB. You may need to mess with the position of your view within the parent view before you add it to get it to display in the right place, but you can do this with calls to -setBounds/-setFrame and/or -setCenter on the UIView.

Share:
12,928
astro8891
Author by

astro8891

Updated on June 19, 2022

Comments

  • astro8891
    astro8891 almost 2 years

    I'm designing a simple Quiz application. The application needs to display different types of QuizQuestions. Each type of QuizQuestion has a distinct behavior and UI.

    The user interface will be something like this: alt text http://dl.getdropbox.com/u/907284/Picture%201.png

    I would like to be able to design each type of QuizQuestion in Interface Builder.

    For example, a MultipleChoiceQuizQuestion would look like this: alt text http://dl.getdropbox.com/u/907284/Picture%202.png

    Originally, I planned to make the QuizQuestion class a UIViewController. However, I read in the Apple documentation that UIViewControllers should only be used to display an entire page.

    Therefore, I made my QuizController (which manages the entire screen e.g. prev/next buttons) a UIViewController and my QuizQuestion class a subclass of UIView.

    However, to load this UIView (created in IB), I must[1] do the following in my constructor:

    //MultipleQuizQuestion.m
    +(id)createInstance {
        UIViewController *useless = [[UIViewController alloc] initWithNibName:@"MultipleQuizQuestion" bundle:nil];
        UIView *view = [[useless.view retain] autorelease];
        [useless release];
        return view; // probably has a memory leak or something
    }
    

    This type of access does not seem to be standard or object-oriented. Is this type of code normal/acceptable? Or did I make a poor choice somewhere in my design?

    Thankyou,

    edit (for clarity): I'd like to have a separate class to control the multipleChoiceView...like a ViewController but apparently that's only for entire windows. Maybe I should make a MultipleChoiceViewManager (not controller!) and set the File's Owner to that instead?

  • astro8891
    astro8891 over 14 years
    Thanks but I'd like to have a separate class to control the multipleChoiceView...like a ViewController but apparently that's only for entire windows. Maybe I should make a MultipleChoiceViewManager (not controller!) and set the File's Owner to that instead?
  • Matt Long
    Matt Long over 14 years
    I think you're making it harder on yourself doing it that way. Delegation is what you have to use regardless so your decision is about where you want your code--in its own class or in the view controller class where it will be used. It's obviously up to you, but I don't think you're gaining anything by placing it in its own class. Best Regards.