3

I am trying to find out which view controller that I came from. Let me explain.

I have viewController A (Place viewcontroller). On viewcontroller A there are 3 buttons (Website button, Map button, Rate button). When a user presses Rate button they go to Rate viewcontroller. When they come back from that viewController I have to 'do stuff' on/within viewController A (Place view controller). When a user presses either the 'Website button' or 'Map button' no action is required.

Is there a way I can check which viewcontroller I have just come from?

Does anyone have any suggestions?

Thanks.

Anthony
  • 879
  • 3
  • 11
  • 17

4 Answers4

3

You could use delegation to do this.

You could define a protocol, say, RateViewControllerDelegate. RateViewController would have a delegate that conforms to this protocol.

Your PlaceViewController would conform to this protocol, which could have a method such as -rateViewControllerCompletedSomeThing, which the RateViewController could send when it is finished. In PlaceViewController's implementation of this method, it could dismiss/pop RateViewController, and do whatever else it is that you want to do when RateViewController has been dismissed.

JoeFryer
  • 2,751
  • 1
  • 18
  • 23
3

One simple way to do this would be to add a class property to A (e.g. BOOL didComeFromRate). Then in Rate VC add something like the code below. Then you can check that property when view controller A loads.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *targetVC = [segue destinationViewController];
        targetVC.didComeFromRate = YES;
}
2

If you open a modal view controller (presentViewcontroller), you can use the 'parentViewController' property of your view controller.

If it's navigation, the navigation controller has stack of view controller in the property 'viewController'.

Best thing in this case is using a protocol, and just set a delegate.

Avi Tsadok
  • 1,843
  • 13
  • 19
2

Yes there is. The UIViewController class declares a property presentingViewController that according to the docs

If the view controller that received this message is presented by another view controller, this property holds the view controller that is presenting it. If the view controller is not presented, but one of its ancestors is being presented, this property holds the view controller presenting the nearest ancestor. If neither the view controller nor any of its ancestors are being presented, this property holds nil.

When using this property from within your UIViewController subclass, you'll want to reference the property like this:

UIViewController *oldVC = self.presentingViewController;
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • I think he wants it the other way around i.e. when returning to the presentingVC from a presentedVC, presentingVC wants to know which VC it has just finished presenting. Aaaand breath. – JoeFryer Dec 18 '13 at 14:33
  • if Place viewcontroller is in a navigationController, and push to Rate viewcontroller. In this situation, does self.presentingViewController works ? – Joiningss Dec 18 '13 at 14:33
  • @Joiningss No, if that's the case you'd be better off using `parentViewController` as @Avi Tsadok suggested. – Mick MacCallum Dec 18 '13 at 14:37
  • @0x7fffffff I had write a test demo,self.presentingViewController not works in navigation. And self.parentViewController is Kind of UINavigationController,so, self.parentViewController both not works. Avi Tsadok didn't suggest to use parentViewController in navigation. – Joiningss Dec 18 '13 at 14:48
  • @Joiningss Correct, but he did suggest using it and I wanted to give him proper credit. – Mick MacCallum Dec 18 '13 at 14:50
  • @0x7fffffff - Thanks. I looked at the method and that is what I am after. I have tried it and when I log it, I just get null. From the thread I am not to sure, does this work? – Anthony Dec 18 '13 at 15:24