Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to persist a user's selection through a series of view controllers?
    primarykey
    data
    text
    <p>I've been reading <a href="https://stackoverflow.com/questions/569940/whats-the-best-way-to-communicate-between-view-controllers">this</a> thread on StackOverflow about dependency injection and am really keen to use the ideas given by the top rated answer. However, I'm having trouble extending them to work in my case and have looked everywhere for a solution but have had no luck.</p> <p>To illustrate my situation in the simplest terms, lets say I have:</p> <p>ViewController A - In which the user picks a photo<br> ViewController B - In which the user then picks a colour<br> ViewController C - In which the image and colour are displayed back to the user </p> <p>I can see how I can use dependancy injection to specify that ViewController A should push ViewController B to the navigation controller once a photo has been selected, but where should I store the photo that was selected? How should I make it available to ViewController C to display?</p> <p>Also, how should I tell ViewController B that it needs to push ViewController C when a colour has been picked?</p> <p>The only solution I can think of which strictly follows the ideas of the above thread is to have a 'photo' property on ViewController B which gets set by ViewController A when it pushes it to the navigation controller. But this seems like a 'bad idea' to me because in general ViewController B should have no interest in photographs whatsoever as its sole purpose in life is to pick a colour and move on.</p> <p>In trying to solve this I have come up with 2 possibilities:</p> <p><em>1/ Make all ViewControllers report back to a central delegate.</em> </p> <p>This seems to fly in the face of what the dependency injection thread says, but it still seems the easiest way. I.e. ViewController A picks a photograph and tells a delegate about it. The delegate stores the photo for later and pushes ViewController B to the navigation controller. When a colour is picked, it stores this and pushes ViewController C, passing it the values for colour and photo it stored previously.</p> <p><em>2/ Pass some kind of properties object between the controllers.</em> </p> <p>I could make an object with 'colour' and 'photo' properties and pass it to ViewController A. The photo property would be set in ViewController A and then the object would be passed to ViewController B, which would set the colour property before passing it on to ViewController C for display.</p> <p>Both options seem kind of 'ok' to me, but neither feel quite right.</p> <p>Maybe I've totally misunderstood dependency injection, in which case please let me know! Otherwise, any thoughts on the above would be tremendous.</p> <p>Thank you in advance!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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