Ryan Hampton
Ryan Hampton

Reputation: 319

Swift: How to segue between view controllers and use navigation bar to go backwards within a child view controller (XLPagerTabStrip)

I am currently implementing the XLPagerTabStrip (https://github.com/xmartlabs/XLPagerTabStrip) which effectively creates a tab bar at the top of the view controller. I want to be able to segue to a new view controller from one of the tabbed controllers and be able to use the navigation bar to move backwards (or a custom version of the navigation bar if this isn't possible).

XLPagerTabStrip provides the moveToViewController and moveToViewControllerAtIndex functions to navigate between child view controllers, but this method doesn't allow use of a navigation bar to go backwards.

Upvotes: 1

Views: 1014

Answers (3)

Ade
Ade

Reputation: 373

To navigate back to your previous view tab controller, you had initially navigated from;

  1. Embed your new view controller, from which you wish to navigate away from in a navigation bar

  2. Connect it's Navigation Bar Button to the Parent view containing the tab bar by dragging a segue between the 2 views

  3. Create a global variable in App delegate to store current index which you will use in the Parent view to determine what tab view controller to be shown

    var previousIndex: Int = 0 //0 being a random tab index I have chosen

  4. In your new view controller's (the one you wish to segue from) viewdidload function, create an instance of your global variable as shown below and assign a value to represent a representative index of the child tab bar view controller which houses it.

    //Global variable instance to set tab index on segue
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.previousIndex = 2
    

You can write this for as many child-tab connected views as you wish, remembering to set the appropriate child-tab index you wish to segue back to

  1. Now, create a class property to reference your global variable and a function in your Parent view as shown below

    let appDelegatefetch = UIApplication.shared.delegate as! AppDelegate
    

The function

func moveToViewControllerAtIndex(){
    if (appDelegatefetch.previousIndex == 1){
        self.moveToViewControllerAtIndex((self.appDelegatefetch.previousIndex), animated: false)
    } else if (appDelegatefetch.previousIndex == 2){
        self.moveToViewControllerAtIndex((self.appDelegatefetch.previousIndex), animated: false)
    }
}
  1. You may now call this function in the Parent View Controller's viewDidLoad, as shown below.

    moveToViewControllerAtIndex()

Run your project and that's it.

Upvotes: 0

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

Conceptually XLPagerTabStrip is a collection of view controllers declared and initialized during the XLPagerTabStrip model creation. It has virtually no sense to use a UINavigationController if you already have all the viewcontrollers available. You can create a global var previousIndex to store the previous viewController index and allow users to go back by using canonical methods:

func moveToViewControllerAtIndex(index: Int)
func moveToViewControllerAtIndex(index: Int, animated: Bool)
func moveToViewController(viewController: UIViewController)
func moveToViewController(viewController: UIViewController, animated: Bool)

About a new viewController, suppose you have 4 viewControllers that built your container (XLPagerTabStrip) named for example z1, z2, z3 e z4. You can embed to z4 a UINavigationController (so it have the z4 controller as rootViewController) and start to push or pop your external views. When you want to return to your z4 you can do popToRootViewControllerAnimated to your UINavigationController

When you are go back to z4 , here you can handle your global var previousIndex to moving inside XLPagerTabStrip.

Upvotes: 0

Westside
Westside

Reputation: 685

I'm not familiar with XLPagerTabStrip, but I had a similar problem recently and the solution was to use an unwind segue to go back to the previous view controller. It's pretty trivial to implement so probably worth a try.

Upvotes: 0

Related Questions