Asi Givati
Asi Givati

Reputation: 1475

Objective C - Detect the instance of the previous modal view

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 ?

Upvotes: 0

Views: 114

Answers (1)

matt
matt

Reputation: 534893

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.

Upvotes: 1

Related Questions