0

I have been using the XLPagerTabStrip to effectively create a tab bar at the top of my view controller, with mutiple child view controllers displayed within it (source : https://github.com/xmartlabs/XLPagerTabStrip)

Lets call these child view controllers A1, B1 and C1. Within the child view controllers are table view which once tapped segue to a new view controller (A2). I want to be able to get from A2 back to the first view controller displaying the child view controllers. I have tried add a navigation bar to both the child view controllers and using the navigationController?.popToRootViewControllerAnimated(true) method, but when a button containing this code is pressed nothing happens. I have also tried embedding a navigation controller within the mother view controller, but this also doesnt work (not sure if this is because the child view controllers A1, B1 and C1 are not connected to the mother view controller within the storyboard).

Any help would be greatly appreciated.

Ryan Hampton
  • 319
  • 3
  • 20

1 Answers1

2

Ok so I think there is a couple ways you can go about this. First the answer depends on what type of segue you're using. If you're using present modally then all you need to do is type

self.dismissViewControllerAnimated(true, completion: completion)

Otherwise see below for methods to segue back to the A1ViewController.

First Option:

You could embed the Mother View Controller in a UINavigationController. The Mother View Controller would then be the root view controller for the UINavigationController and you would be able to use

navigationController?.popToRootViewControllerAnimated(true)

Second Option:

An unwind segue. These segues are perfect for going backwards and can be implemented in 3 steps.

  1. Create an object for the user to select to go back (In your case this would be that UIButton in A2)
  2. Create an unwind method in the controller you want to return to. (In your case the method would be in controller A1)

    @IBAction func unwindToA1ViewController(segue: UIStoryboardSegue)
    
  3. Hook up the method in UIStoryboard. Go to the A2 scene and control drag the A2ViewController to Exit and then select the unwind method you created in step 2. (At the very top of the A2 ViewController)

See below for a further explanation and great visuals! https://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2

Hope this helps!

Richard Ash
  • 352
  • 3
  • 6