How do I go back to the selected tab bar view controller from a modally presented view controller?

Viewed 195

enter image description here

From the tab bar controller I want to modally display 2 other view controllers (full screen). After I’m done with full screen 1 & 2 I want to go back to the selected tab in the tab bar controller.

However, when I use:

Self.view.window.rootviewcontroller.dismiss(animated: true, completion: nil)

It takes me back, but "viewdidappear" doesn't re-run. How can I present the selected tab bar VC again so that “viewdidappear” runs again?

1 Answers

Instead of depending on viewdidappear to be called on dismissal of two full screen view controllers. I would suggest you delegate the task of refreshing tab bar content on dismissal of two fullscreen.

For example:

    //fullscreen 2 dismiss action
         func done()
        {
           dismiss(animated: true, completion: {
                  //here you can have reference to presenter view controller as delegate and pass your info to that delegate
                   var info = some info captured from full screen two view controller.
                   delegate.dismissAndRefresh(info:info)
                }
        }

//In Full screen 1 view controller
   func dismissAndRefresh(info: Dictionary)
   {
      dismiss(animated: true, completion: {
          //have reference to tab bar controller and pass the info to tab controller.
      tabBarControllerDelegate.refreshContent(info)

      }
   }
//In tab controller
func refreshContent(info:Dinctionary)
{
  //refresh your content as you are doing in view did appear.
}

This is not the only way you can try directly passing the info to tab bar controller from the full screen 2 view controller and dismiss both presented full-screen view controllers.

But fetching root view controller using a self-view window will cause a lot of problems when you view hierarchy changes, for example, some other controller being presented or some alert being displayed.

Related