Jack Philips
Jack Philips

Reputation: 57

Know that a ViewController appears because of a Dismissal (not segue)

I'm trying to figure out how to grab a class that just dismissed itself, inside my rootViewController. I have several options to dismiss back to my rootViewController and I need to know if it came from one instance in order to change a bit of UI accordingly.

I'm looking for something similar to [self presentingViewController] except for when the viewController appears because of a dismissal and not a segue. Is there a built in method for grabbing this?

Upvotes: 1

Views: 185

Answers (2)

Jack Philips
Jack Philips

Reputation: 57

I basically fixed this problem by keeping track of whether I even visit the one view controller I was concerned about dismissing from. That way, I just set a property in the viewController before I segue so I already know when I return that I need to take care of the UI changes. Thank you for the comments on unwind segues. I will definitely be implementing them in the future very soon!

Upvotes: 2

foundry
foundry

Reputation: 31745

update

Everything in this answer is old news - since XCode 4.5 we can use unwind segues to get back to any previous viewController and trigger an unwind method in that controller. (thanks @rdelmar)

What are Unwind segues for and how do you use them?


the old way/code way.. which ideally involves delegates to get specific methods implemented

When a class dismisses itself, you can't grab it because it is ... dismissed. You need to have a hold of it before it is dismissed, and then know about the dismissing.

Elaborating on this a little, classes don't usually dismiss themselves, their owning classes do the dismissing. The obfuscating method here could be the UIViewController method:

- (void) dismissViewControllerAnimated:

which is a shorthand for

- (void) [[self presentingViewController] dismissViewControllerAnimated:completion:nil]

The presenting viewController has a property presentedViewController which holds on to that dismissed object - until it is dismissed. When the presentingViewController dismisses, it resets it's presentedViewController property to nil. But you always have the option of copying that reference into another (strong/retained) property prior to, and interrogating it after, the dismissing event.

To quote apple:

"If you want to retain a reference to the receiver’s presented view controller, get the value in the presentedViewController property before calling [ dismissViewControllerAnimated:completion: ]."

Upvotes: 1

Related Questions