ios: how to dismiss a modal view controller and then pop a pushed view controller
Asked Answered
F

1

13

I have a view controller B that is pushed onto the navigation stack by root view controller A and this view controller B needs to display an alternative view if its model is in a certain state so it is modally presenting another view controller C. When I dismiss view controller C I would also like to pop view controller B if that is also on the nav stack. I would like to do it in such a way that there is only 1 transition.

Any ideas?

Floriated answered 1/5, 2013 at 1:2 Comment(2)
Are you using xibs, or storyboard to layout your views? It sounds like your best bet may be to instantiate the tertiary views within your root view controller, and then add them as subviews. These can then be shown or hidden. This can be a lot more flexible than presenting and dismissing modal views.Fernanda
Im using a storyboard, I was leaning towards using a seperate viewcontroller to display the alternative state because the apple documentation mentioned that if you have an alternative interface to display then its worth presenting it as a seperate view (it mentioned this in the discussion of separate landscape views). I think the actual ui changes I need would be easier to implement via seperate view that gets unhidden on the current view, so i will try your suggestionFloriated
D
20

In the scenario you posted, the presenting view controller for view controller C will actually be the navigation controller, so you can ask it to pop off B, and then dismiss yourself. This code is in view controller C:

-(IBAction)goBackToA:(id)sender {
    [(UINavigationController *)self.presentingViewController  popViewControllerAnimated:NO];
    [self dismissViewControllerAnimated:YES completion:nil];
}

If you are using a storyboard, you can do this same thing, jumping directly back to A with an unwind segue.

Daveen answered 1/5, 2013 at 2:32 Comment(1)
thanks, after giving it some thought I think what I need might be more easily done using just one view controller and changing its subviews according to the model state, I will try your approach if I need to use 2 seperate view controllersFloriated

© 2022 - 2024 — McMap. All rights reserved.