views:

34

answers:

1

In the iPhone app I'm currently working on, I'd like two view controllers (I'll call them A and B) to have toolbars that are identical in appearance and function. The toolbar in question will look like this:

[(button) (flexible-space) (label)]

For posterity's sake, the label is actually a UIBarButtonItem with a custom view. My design requires that A always appear directly before B on the navigation stack, so B will never be loaded without A having been loaded. Given this layout, I started wondering,

"Is it worth it to re-use A's toolbar items in B's toolbar?"

As I see it, my options are:
1. Don't worry about re-use, create the toolbar items twice
2. Create the toolbar items in A and pass them to B in a custom initializer
3. Use some more obscure method that I haven't thought of to hold the toolbar constant when pushing a view controller

As far as I can see, option 1 may violate DRY, but guarantees that there won't be any confusion on the off chance that (for example) the button may be required to perform two different (no matter how similar) functions for either view controller in future versions of the app. Were that to happen, options 2 or 3 would require the target-action of the button to change when B is loaded and unloaded. Even if the button were never required to perform different functions, I'm not sure what its proper target would be under option 2.

All in all, it's not a huge problem, even if I have to go with option 1. I'm probably overthinking this anyway, trying to apply the dependency injection pattern where it's not appropriate. I just want to know the best practice should this situation arise in a more extreme form, like if a long chain of view controllers need to use identical (in appearance and function) UI elements.

+1  A: 

It really does depend on how things might change in the future. Assuming for the moment that (at most) the text in the label and the targets for the actions might change, I would lean in the direction of reuse. What I actually did in this case was create a common base class for my view controllers that had a utility method for creating the toolbar items. They can either be created programmatically or pulled from a nib. Either way, this is pretty flexible for future reuse (just subclass another controller) or divergence (remove the superclass relationship and implement your situation-specific toolbar on the controller that has changed).

warrenm