1
votes

I have an app which has a UINavigationController, which has a couple of UIViewControllers. Each ViewController has its own nib.

The two view controllers are 90% identical. So currently, there is a lot of code duplication. So I'm wondering is there a way I can share the code that deals with each nib?

In other words, a class that can be passed between the view controllers, that can manage all the interface elements?

2
Write a single UIViewController with the common code, and then subclass it. See optictheory.com/iphone-dev/2010/03/…. If you have to share data between the two views, then you basically have to do a little bit more of a mess, create a singleton: iphone.galloway.me.uk/iphone-sdktutorials/singleton-classesnflacco

2 Answers

1
votes

You might consider using a single UIViewController class, since they are so similar. In the init method, just have it load the nib you need for that instance.

Another option would be for one of the UIViewController to extend the other.

Either way, less code is usually better.

1
votes

I've seen an example where each ViewController inherits from a Class wich has as property another class, wich is the main class, this class (the main) has the delegates needs for both views.