iosswiftuinavigationbarchildviewcontrollerxlpagertabstrip

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.


Solution

  • 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.