Search code examples
objective-cviewcontrollers

2 UIViewController 1 View


This is a question regarding iPhone objective C design. How can I have 2 UIViewControllers manage the same view? Each UIViewController will be created at different times, but their views are essentially the same thing.

To help explain, here's an example:

Let's say I have one ViewController that synchronizes a list of tasks from the server, allows the user to edit/delete these tasks, and displays other information on the view (like status messages), etc. At a separate time, I want to show a "read-only" list of those tasks using the same view (since the view has already been created, tableView is created, status message textbox is there etc.), but based on different things, I want this view to act differently and update different status messages.

So in this example, I want to reuse the view but use 2 different controllers depending on where the user is. If he/she is in edit mode, display UIViewController 1 which manipulates VIEW 1. If he/she is in read mode, display UIViewController 2 which manipulates VIEW 1.

I'm wondering the best way to design this if this is 1. possible or 2. should I just duplicate VIEW 1 as VIEW 2 and have UIViewController 2 manage VIEW 2, even though they'd be the exact same.

Thanks in advance for your help.


Solution

  • Create a base UIViewController class that each of yours descend from. Put all of the common interface element declarations (either plain ivars or IBOutlets) in the base class. If you use InterfaceBuilder then make the files owner the base class.