Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I re-use UI elements across view controllers?
    primarykey
    data
    text
    <p>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:</p> <p><code>[(button) (flexible-space) (label)]</code></p> <p>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,</p> <h3>"Is it worth it to re-use A's toolbar items in B's toolbar?"</h3> <p>As I see it, my options are:<br> 1. Don't worry about re-use, create the toolbar items twice<br> 2. Create the toolbar items in A and pass them to B in a custom initializer<br> 3. Use some more obscure method that I haven't thought of to hold the toolbar constant when pushing a view controller</p> <p>As far as I can see, option 1 may violate <b>DRY</b>, 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.</p> <p>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 <b>dependency injection pattern</b> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload