Search code examples
objective-cuiviewcontrollerpushviewcontrollerpopviewcontrolleranimated

Which UIViewController called popViewControllerAnimated:?


Lets say I have 3 UIViewControllers A, B, C.

Now lets say I call B or C from A. Not with segues, but with instantiating them in code and pushing them to the navigationController.

After B or C are done, they call popViewControllerAnimated: and therefore return to A.

At this point I would like to access a property of either B or C, depending on which one of them actually was the last one that I used.

Is there any method or property that would give me the information if the last UIViewController was of type B or C?

I know I could define a protocol for B and C and set A as the delegate, but I was looking for something else...


Solution

  • As inconvenient as it can be to set up every time, the delegate pattern is the correct way to solve this. That said, the quickest, hackiest way of getting there quickly is probably just to keep the reference to the last viewController you pushed in an instance variable or property of A, then make use of this in -viewWillAppear:. But making sure this property is always correct once things start getting complicated is probably more work in the end than setting up a simple delegate.