1

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?

Jack Philips
  • 57
  • 1
  • 10
  • not very clear what you want. maybe you can check these methods: `isMovingFromParentViewController`, `isMovingToParentViewController`, `isBeingDismissed` and `isBeingPresented` – Bryan Chen Feb 26 '13 at 23:15
  • Is there a reason why you can't use a segue? An unwind segue would be great for the is, because you can get the sourceViewController property of the segue being performed.. – rdelmar Feb 26 '13 at 23:21

2 Answers2

2

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!

Jack Philips
  • 57
  • 1
  • 10
1

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: ]."

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125