Should I re-use UI elements across view controllers?

Posted by Endemic on Stack Overflow See other posts from Stack Overflow or by Endemic
Published on 2010-05-19T20:17:27Z Indexed on 2010/05/19 20:20 UTC
Read the original article Hit count: 137

Filed under:
|

In the iPhone app I'm currently working on, I'd like two navigation 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.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa-touch