0

I have 3 views view1, view2 and view 3. i have a problem of changing a data in view 2 when i am in view 3. here are the ways i can think of

  1. pull the navigation controller stack, identify the view by iskindof test, take the variable and change it
  2. view 2 get the value from appdelegate and from view 3 i just update it in appdelegate and it works
  3. use a notification center and change the data.

which is the most advice way to change the data in my case.

the data is just a change to set of values to array of objects . .

thndrkiss
  • 4,515
  • 8
  • 57
  • 96
  • 1
    [What's the best way to communicate between view controllers?](http://stackoverflow.com/q/569940/194544) – beryllium Mar 26 '12 at 07:52

1 Answers1

1

Option (1) is the approach with the highest risk of giving you a headache if you do any refactoring further down the line. It can be fiddly too, so there's a risk of making mistakes and then having to expend more time to fix them. For these reasons, it may be best to avoid it.

Choosing between (2) and (3) tends to provoke ideological debates about globals, encapsulation etc.

There's a lot to be said for (3), using UINotificationCenter, even if you don't adopt the view that globals are evil. It encourages you to think very clearly about the communication protocol you are adopting, it prevents clutter in the the app delegate, it tends to be the most stable for refactoring, if you ever want to re-use the views elsewhere you have a head start etc.

In particular, with well designed notifications, you often find that if you refactor further down the line you'll have much less work - or even none - to keep your code running right.

Obliquely
  • 7,002
  • 2
  • 32
  • 51