Search code examples
iosswiftuiviewcontrolleruitabbar

Dismiss View Controller within UITabController


I have the following stack on my Storyboard

-> Navigation Controller 
 -> MyCoursesViewController (does a prepareForSegue to next 'view')
  -> Course Info UITabController (I select Setting Tab)
   -> Setting Navigation Controller
    -> SettingViewController

When I am in the Setting Tab: SettingViewController, I have a UIButton which says ‘Exit Course’ and I want it to go back to MyCoursesViewController. How can I pop to reach this view controller?

I tried the things below:

self.tabBarController?.navigationController?.dismissViewControllerAnimated(true, completion: nil)
self.navigationController?.tabBarController?.dismissViewControllerAnimated(true, completion: nil)
self.dismissViewControllerAnimated(true, completion: nil)
self.tabBarController?.navigationController?.popViewControllerAnimated(true)
self.navigationController?.popViewControllerAnimated(true)

but to no avail.

Is there a way to simply dismiss or do I need to implement a delegate from MyCoursesViewController to pass to the SettingViewController via the UITabController? I am not sure how to pass a delegate to MyCourses -> Course Info Tab -> Setting. If it was MyCourses -> Setting, that’s just a simple delegate pass from class to class but the confusing part is the intermediate Tab controller.


Solution

  • From your description, it sound like MyCoursesViewController is the rootViewController of your NavigationController.

    You pushed the CourseInfoUITabController to your stack and your SettingViewController is actually embedded in a UINavigationController therefore you can do this:

    self.navigationController?.tabBarController?.navigationController?.popViewControllerAnimated(true)