0

I'm using the standard code for present & dismiss modal segue

[self dismissViewControllerAnimated:YES completion:nil];

I want to perform some method of the previous view, in the completion area of the current view.

for ex. the previous view is a menu with profile pic and more stuff, and the current view is view for changing the profile pic. when I dismiss the current view I want to update the UIImageView of the profile at the menu view (the previous view).

Is it possible to get the instance of the previous view ?

Asi Givati
  • 1,348
  • 1
  • 15
  • 31
  • See "Passing Data Back" section of [Passing Data between View Controllers](http://stackoverflow.com/a/9736559/1271826). – Rob Oct 12 '14 at 13:58

1 Answers1

1

The usual thing is for your second view controller to define a protocol and a delegate that adopts that protocol, and for your first view controller to make itself the second view controller's delegate. Now the second view controller has a reference to the first (because it is its delegate) and is guaranteed of a method or methods that it can call to hand information back to the first (because the delegate has adopted a protocol).

The question then arises of when the first view controller will set itself as the second view controller's delegate. There are two situations:

  • The first view controller creates the second view controller in code. Obviously it now has a reference to second view controller and can set itself as its delegate.

  • You're using a storyboard and this is a modal presentation segue. In that case, implement prepareForSegue in the first view controller. Now you can get the segue's destinationViewController - that's the second view controller, so the first view controller can now set itself as the second view controller's delegate.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • For an example, see the end of this section of my book: http://www.apeth.com/iOSBook/ch19.html#_presented_view_controller – matt Oct 12 '14 at 13:47
  • How do I need to initialize delegate? I mean - I know how to set delegates etc.. but in the second view I need to write 'previousViewInstance.delegate = self' but how can I get the previousViewInstance? (sorry if I'm not so clear :) – Asi Givati Oct 12 '14 at 14:06
  • 1
    This answer helped me a bunch http://stackoverflow.com/questions/6203799/dismissmodalviewcontroller-and-pass-data-back – MendyK Oct 12 '14 at 14:08
  • btw Matt, are you THE matt neuberg? if so, thanks for literally the best programming books on the planet :) – MendyK Oct 12 '14 at 14:09
  • @NewEngland Thanks, the check is in the mail! :) – matt Oct 12 '14 at 14:37
  • 1
    @AsiGivati I'll expand my answer a little to answer that. – matt Oct 12 '14 at 14:38
  • @matt have you made other vids like this? www.youtube.com/watch?v=zIufcKpDIRo I found this one amazingly useful! – MendyK Oct 13 '14 at 00:35