Reputation: 1974
I have been using the XLPagerTabStrip to effectively create a tab bar at the top of my view controller, with multiple child view controllers displayed within it. Lets call Home as parent controller which extends ButtonBarPagerTabStripViewController. There are two child view controller, Child1 and Child2. Everything working perfect as I expected. But now I need a requirement, in which I have to switch child view controllers programmatically. ie, when a process(in child2) finished, it should trigger the switching the tab from Child 2 to Child1. I know there are couple of methods,
func moveToViewController(at index: Int)
func moveToViewController(at index: Int, animated: Bool)
func moveTo(viewController: UIViewController)
func moveTo(viewController: UIViewController, animated: Bool)
but I don't know how to use it.
Upvotes: 2
Views: 3563
Reputation: 2305
Here is the perfect solution to move any specific childview controller. You just need some delay to change viewcontroller
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.moveToViewController(at: 1, animated: true)
}
call this lines of code inside viewDidLoad()
method.
Upvotes: 1
Reputation: 246
**These methods can be used as following:
Will show how to use these functions through delegate
func moveToViewController(at index: Int) :
You need to pass the index of the target child controller, for this you can either call this func from your child controller by initialising the parent controller or you can set a delegate which will be implemented in your parent controller and should be triggered from your child view controller, ultimately it will select the target controller with animation.
protocol MyDelegate {
func selectChildControllerAtIndex(toIndex: Int!)
func selectViewController(viewController: UIViewController)
}
ChildProcceedPressedDelegate needs to be conformed in your parent controller
In selectViewController you need to pass the reference of your target controller from your child controller, which you can do by storing it somewhere and passing it.
internal func selectViewController(viewController: UIViewController) {
moveTo(viewController: viewController, animated: true)
}
In selectChildControllerAtIndex you just need to pass the index of your target controller:
func selectChildControllerAtIndex( toIndex: Int!) {
self.moveToViewController(at: index, animated: true)
}
func moveToViewController(at index: Int, animated: Bool) :
It works same as above method but in this you can pass if you want to animate to your target controller or not. Use delegate: selectChildControllerAtIndex
func moveTo(viewController: UIViewController)
It works by passing your target child controller reference and it will animate to your target controller. by delegate: selectViewController
func moveTo(viewController: UIViewController, animated: Bool)
It works by passing your target child controller reference and it will animate as per your choice to your target controller. by delegate: selectViewController
Upvotes: 1
Reputation: 522
The method is pretty straight forward, but you need to call it in your Home(parent View Controller)
In your Child2 view controller class, write the code as below
//after some process done here
let parentViewController = self.parent! as ParentViewController
parentViewController.moveToViewController(at: 0) //Assume your child1 view controller is on index 0
Upvotes: 8