views:

686

answers:

3

Hi,

I'm developing an iPhone app. I need to create a Quiz application that has different Question views embedded in it (see my similar question).

Different types of Question will have different behavior, so I plan to create a controller class for each type of Question. The MultipleChoiceQuestionController would set up a question and 3-4 buttons for the user to select an answer. Similarly, the IdentifyPictureQuestionController would load an image and present a text box to the user.

However, the docs say that a UIViewController should only be used for views that take up the entire application window. How else can I create a class to manage events in my subviews?

Thanks,

+1  A: 

Subclassing UIViewController will provide this functionality. For example, MultipleChoiceQuestionController would be a subclass of UIViewController. MultipleChoiceQuestionController would contain the question text (UILabel or UITextView) and several buttons (UIButton). You could create a custom constructor in MultipleChoiceQuestionController that would fill the view with the relevant question string and other relevant info.

When you want to add MultipleChoiceQuestionController's view to your main view's subview, simply do the following:

[myMainView addSubview:instanceOfMultipleChoiceQuestionController.view];
zPesk
Apple says not to: "Note: You should not use view controllers to manage views that fill only a part of their window—that is, only part of the area defined by the application content rectangle. If you want to have an interface composed of several smaller views, embed them all in a single root view and manage that view with your view controller.(http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-DontLinkElementID_1)
brainfsck
A: 

You can handle the events on the view itself, or your view controller could have a delegate class that changes for different types of question. That delegate would process the different input, and react in a different way to user touches.

Here's some code with the idea.

// In QuestionViewControllerDelegateProtocol.h
@protocol QuestionViewControllerDelegateProtocol

// Define the methods you want here
- (void)touchesBegan;
- (void)touchesEnded;
- (void)questionLoaded;

@end

// In QuestionViewController.h
@interface QuestionViewController {
    id<QuestionViewControllerDelegateProtocol> delegate;
}

@end

// In QuestionViewController.m
@implementation QuestionViewController

- (void)viewDidLoad:(BOOL)animated {
     [delegate questionLoaded];
}

- (void)touchesBegan {
     // Some processing logic.
     [delegate touchesBegan];
}

@end
pgb
It would make sense to have the delegate class extend UIViewController, but the Apple docs tell me not to. Then how can I make methods like viewDidLoad: fire?
brainfsck
I'm not talking about the App Delegate here. I'm talking about a custom delegate class, that does part of the work your `UIViewController` doesn't. This is so you can swap that delegate based on the view selected (the type of question)
pgb
Thanks. So I'm thinking of using loadNibNamed:@"MySubview" owner:myCustomDelgate . Will myCustomDelegate be notified when the subview is loaded?
brainfsck
It will be up to you to notify it.
pgb
I added some sample code with a skeleton of the idea.
pgb
Thank you
brainfsck
A: 

I have the same problem, and according to Apple's doc, here's what you should do:

Note: If you want to divide a single screen into multiple areas and manage each one separately, use generic controller objects (custom objects descending from NSObject) instead of view controller objects to manage each subsection of the screen. Then use a single view controller object to manage the generic controller objects. The view controller coordinates the overall screen interactions but forwards messages as needed to the generic controller objects it manages.

http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html#//apple_ref/doc/uid/TP40007457-CH112-SW12

Greg Schlom