0

I have a root view controller (RVC) that opens up a Modal ViewController (MVC). I then navigate within the MVC to few more VC's via a push. What is the best practice to get from one of those VC's back to the RVC?

Normally I have a delegate from the Modal VC that calls up to the RVC which then dismisses the modal, but if you navigate away from it, but I'm not sure how I would do that if you navigate away from it.

4
  • Is your MVC a UINavigationController? Commented Aug 9, 2012 at 14:46
  • have you tried the popToRootViewControllerAnimated: method yet? Commented Aug 9, 2012 at 14:58
  • @holex that won't work; don't forget he is inside modal view controller (possibly wrapped in UINavigationController), by popping to root would get him to the first view controller inside the modal view but that wouldn't dismiss the modal view, which is what he is aiming for. Commented Aug 9, 2012 at 15:02
  • @Peter Yes the MVC contains a UINavigationController. Commented Aug 9, 2012 at 15:24

2 Answers 2

1

Without seeing any code it is a bit hard to help but let me shoot in the dark here.

I will assume that the first controller presented inside the modal view provides the protocol/delegate to call the dismiss action.

If you use UINavigationController inside your modal view to push other view controllers on the stack you can always obtain the first controller like this

UIViewController * yourFirstController = [[[self navigationController] viewControllers] objectAtIndex:0]; // and then use your delegate to call your dismiss method // you will need to typecast your controller based on your subclass otherwise will get warning here if ([[yourFirstController delegate] respondsToSelector:@selector(yourCloseProtocolMethod)]) { [[yourFirstController delegate] yourCloseProtocolMethod]; } 
Sign up to request clarification or add additional context in comments.

2 Comments

I think that will work, but seems a little fragile. Is that the standard pattern for doing this kind of thing or is it non-standard to navigate within a modal and dismiss back to the true root.
In my experience it is quite common. Modal view is meant to be used to interrupt from regular flow of an app. For instance creating new item etc., and very often the modal view then needs detail views. You can find this type of behaviour in Apple applications as well.
1

Don't forget that a delegate doesn't have to be a property of a UIViewController inside your model navigation stack. Consider creating a singleton class that holds a reference to the rootviewcontroller as a delegate. That way any class in your application has access to it and you aren't forced to continually pass it through to every UIViewController that requires it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.