Search code examples
iosuitabbarcontrollermodalviewcontroller

Dismiss modal view and call initial view controller


this is my problem. I have a tab bar, in the last tab I load a modal view. When I dismiss the modal view the app returns in the last tab of the tab bar. But instead I'd like that the app returns on the first tab of the tab bar (the initial view). if I dismiss the modal view the code that I insert after (to call initial view controller) is not taken in account. Can you offer me a solution? Thank you.

The code that I use is:

  [self dismissViewControllerAnimated:YES completion:nil];
  InitialViewController* controller = (InitialViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"Initial"];
  [self presentViewController:controller animated:NO completion:nil];

Solution

  • What you are doing is not "returning" to the tab bar. Instead, you are presenting a completely new tab bar. Now you have two tab bar interfaces. Don't do that.This is sufficient to dismiss:

    [self dismissViewControllerAnimated:YES completion:nil];
    

    To change tabs, you need a reference to the existing tab bar controller (not a different one). Then you can just say:

    [theTabBarController setSelectedIndex:0];
    

    If you know for a fact that you want to do that when you return from the modal controller, you can even do that at the time you present the modal controller.