-1

I have a Main Menu-type View Controller (let's call it MainVC) with 2 buttons - one modally presents another regular VC (SourceVC); the other pushes a Table VC (DestinationVC). SourceVC and DestinationVC are NOT directly connected.

Whenever the SourceVC performs MethodX, it creates an object. I want SourceVC to send that object to DestinationVC when MethodX ends so that it can be stored in its table. My questions are:

  1. Is there a way to pass the object directly from Source to Destination (i.e. delegate)?

  2. If not, then how should I go about passing this data?

Edit Just for clarity's sake, I want to emphasize that the VCs passing data are NOT connected/transitioning directly.

The question of passing data between VCs was asked many times before; however, in each instance, the VCs were DIRECTLY connected (i.e. SourceVC Pushes/Modally Presents DestinationVC). I wanted to know if it was different to send data INDIRECTLY, (in my case, Main View Modally Presents SourceVC, Main View Pushes DestinationVC). I know how to do the former, but I'm having trouble when the VC's are separate. Sorry for any confusion.

summerjw
  • 3
  • 5
  • 1
    possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Gil Sand May 26 '15 at 15:16
  • 1
    This is one of the most asked questions for iOS Development on SO. Have you checked some of the other answers? If so, what are your specific problems? – Logan May 26 '15 at 15:17
  • Hi summerjw ! Even though your question is well explained, quite clear and nicely written, it probably has about a thousand duplicates either here on SO or on the internet. I suggest you make sure to research as much as you can before asking here. And only then if you struggle for something specific, ask :) – Gil Sand May 26 '15 at 15:18
  • I'd research a little better before posting on here. This question has been asked time and time again on here. You're a new user, and it's encouraged to ask questions, but please research a bit before you ask! You were clear with your question, and well versed, and that is very much appreciated! Good luck and read @Zil comment.. – apollosoftware.org May 26 '15 at 15:21
  • Yes, I've seen the question asked a lot myself, however, in each instance, the view controllers are **DIRECTLY** connected (i.e. SourceVC Pushes/Modally Presents DestinationVC). I wanted to know if it was different to send data **INDIRECTLY**, (in my case, Main View Modally Presents SourceVC, Main View Pushes DestinationVC). I know how to do the former, but I'm having trouble when the VC's are separate. Let me know if I'm being clear enough. – summerjw May 26 '15 at 15:25
  • And thanks for the replies, I can't say I didn't expect to get this response, but I honestly did read previous questions. Sorry for any misunderstandings! – summerjw May 26 '15 at 15:27
  • @summerjw In that case, you would pass the data *back* from SourceVC to MainVC, then *forward* from MainVC to DestinationVC. See http://stackoverflow.com/a/5210861/3160849 for passing data *forward* and *back*. – johnpatrickmorgan May 26 '15 at 15:28
  • To expand a little on the link provided by johpatrickmorgan. The pass forward in that link is assuming you are passing data at VC creation time. In your case you are passing once the VC is already created. So its not enough just to pass the data, the receiving VC also needs to know its been sent so it can refresh its view (I presume that's what your going to do). So you can for example use delegation for that between the parent and target VC (but the 'other way round' from what might be usual). Or you can use KVO for the target to notice when the parent has set the data object.And then refresh – Gruntcakes May 26 '15 at 15:42
  • @Gruntcakes Both your and johnpatrickmorgan's answers were very helpful. It looks like passing the data via the parent would be the best option (as both of you suggested), but just for future reference, would notifications pass the data directly from two separate VCs (you don't need to go in depth, a simple answer will help)? – summerjw May 26 '15 at 15:49
  • Ok, thank you both again for the explanation/link :) I'll definitely do more some research on this based on what you said. – summerjw May 26 '15 at 15:54
  • @summerjw. Yes that's also an option. You could use that too instead of going via the parent view controller. Using notification is good to decouple objects from each other (but there's no need to decouple objects which are always intended to be tightly used together anyway). In your case, use whichever way you feel is easiest for you to do. Notification is slightly cleaner but you have to package and unpackage the data into the notification object. – Gruntcakes May 26 '15 at 15:54
  • @Gruntcakes Alright, my main goal was to find a direct way of passing data between VCs, but I'll experiment with both options. If you want, you can submit your answer and I'll check it. – summerjw May 26 '15 at 16:10
  • Done. collected comments into an answer – Gruntcakes May 26 '15 at 16:17
  • Good, thanks again everyone for the help! – summerjw May 26 '15 at 16:23

1 Answers1

0

You could go via the parent view controller or use notifications.

See this past question for info on passing data forward and backwards:

stackoverflow.com/a/5210861/3160849

But note that in that answer it is assuming you are passing data at VC creation time. In your case you are passing once the VC is already created. So its not enough just to pass the data, the receiving VC also needs to know its been sent so it can refresh its view (I presume that's what your going to do). So you can for example use delegation for that between the parent and target VC (but the 'other way round' from what might be usual). Or you can use KVO for the target to notice when the parent has set the data object.And then refresh.

Alternatively you could use a notification. You could use that too instead of going via the parent view controller. Using notification is good to decouple objects from each other (but there's no need to decouple objects which are always intended to be tightly used together anyway). In your case, use whichever way you feel is easiest for you to do. Notification is slightly cleaner but you have to package and unpackage the data into the notification object.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378