Comunicación arbitraria entre controladores de vista en una aplicación basada en guiones gráficos

Given a storyboard-based application, how can any one view controller invoke the methods of any other (to gain access to its encapsulated information) without executing the prepareForSegue method (which only passes the destination vc's reference via the segue argument)? I understand that information can be passed from a parent vc to a child vc but I need a more generic approach for any vc to pass information to any other at any point in time.

Put another way, while in vc A, I would like to invoke a method in vc B (that is not named in any segue from A), but don't understand how to get a reference to B from A. Creating a new segue to it would create a new vc; not what I want to do.

I would think there is some way via the storyboard or its associated named segue functions to track down a reference to any vc from any other but have struggled to find a way to do so. In the deeply embedded world I normally live in, I would simply create a singleton class so it could be instantiated from anywhere but that seems inappropriate (if even possible) here for the case of a view controller.

Cualquier ayuda sería muy apreciada.

preguntado el 01 de febrero de 12 a las 03:02

1 Respuestas

I would suggest you not try to do this via storyboard segue functions. Try using NSNotifications. You'll have to code these yourself (i.e. not in Storyboard), but they will allow you to broadcast a "request" that VC-B invoke a method, based on what information you pass it in the notification. VC-B has to be listening for it (observing), but this is a very nice way of de-coupling your classes, but still communicating in an arbitrary way.

Respondido 01 Feb 12, 07:02

Thanks for your timely and straightforward response. I had considered using notifications as a viable candidate and have certainly used them in the past. I also considered creating a singleton class that simply held all vc reference addresses that could then be instantiated anywhere. Thanks again for sharing your suggestion. - Randy Rausch

Your singleton with refs to all vs's violates loose coupling, so I'd avoid that too. I know I keep harping on this, but any time you tie 2 (or more) classes together, you should have a really good reason. - Rayfleck

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.