Managing subviews in Cocoa Touch

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,

Answers


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

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];

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


This is a very nice little solution, gives you all of the advantages of a view controller without breaking apples rules.

From the page:

This is a generic controller class that can be used to handle a subarea. It is modelled after UIViewController, but conforms to Apple's recommendation.

Your view controller creates the instances and is responsible for managing the subview controllers. Alternatively you can further subdivided your view hierachy and create subview controllers inside other subview controllers. In both cases the controller instantiating the object is responsible for managing the subview controller. The responsible controller is referred to as 'parent controller.' Subclasses can use the view controller when they for example need to show a modal dialog.

https://github.com/Koolistov/Subview-Controller


Need Your Help

Java text based application

java swing text terminal jtextcomponent

The main idea is to try and make an application that would have look-and-feel of a standard Linux-type terminal. Now I am limited to Java and here is what I want.