Leon Storey
Leon Storey

Reputation: 3424

MVC Design Pattern - Can a Controller have multiple views which have their own controller?

I have a UIViewController which contains the base view. The view should differ based on the Model, such as load different subviews dependant on the type from a Book Model.

If the Book Model could be of type Paperback or Magazine, then should the current object be of type Magazine the current view should display a subscription box subview.

The subscription box subview contains various UIKit elements which require a controller (e.g. UITextfield, UITableView), the subscription box should only report back to the main controller with an abstracted and simplified results (such that the main controller shouldn't need to control the UITextField and UITableView directly), example of target-action could be didRequestSubscription:(SubscriptionRequest *)subscription which SubscriptionRequest contains their card details and subscription period taken from the input on UITextField and the selected row from UITableView.

Am I right in thinking that a controller can add a subview which itself has a controller? Would this be against the MVC design pattern?

I have attempted to visualise the possibility of this

enter image description here

Upvotes: 2

Views: 2221

Answers (3)

artud2000
artud2000

Reputation: 544

You can do this and it's perfectly valid. But you might want to add your controller as an observer. "Read Observer design pattern".

Pretty much you will subscribe the viewController to the views and the viewController will be notified of the events happening in your view. you will push the notification from the view and observe it from any viewController that has to know about the event.

For example your view controllers will subscribe as follows:

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(infoReceived:) name:@"data saved" object:nil];

And your view will be pushing the events as follows:

NSNotification *registerNotification = [NSNotification notificationWithName:@"data saved" object:self userInfo:registerInfo];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotification:registerNotification];

Notice that the name of the notification is "data saved" then I can register as much view controllers as I want to that event that occurs in a given view.

I hope this helps and it doesn't break MVC it's a really popular design pattern call "Observer" really useful you can work a similar solution using delegates.

Upvotes: 1

Daddy
Daddy

Reputation: 9035

This was just asked here: iOS - Using several view controllers for a single screen

In iOS5+ APIs got added to address this. Before iOS5 it was hacky to use a viewController to manage other viewControllers.

Take a look at the UIViewController instance method for -addChildViewController

Upvotes: 1

Wain
Wain

Reputation: 119031

Yes. That is exactly what childViewControllers is for. Any UIViewController can act as the host to views it directly controls and views that are controlled by other UIViewController instances.

From an MVC point of view, the idea is to separate the data from the control from the display. If the control and display are separated into multiple parts that's just (hopefully good) compartmentalisation.

Upvotes: 2

Related Questions