Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear Navigation stack of viewcontroller

I have a tabBarController with four tabs. From each tab, I can navigate through a series of view controllers. And at the last view controller, I have a 'Done' button, clicking on which I have to be redirected to my initial tabBarController. The code I am using currently to do this is as follows (on button click).

let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
if let tabViewController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as? UITabBarController {
    self.navigationController!.pushViewController(tabViewController, animated: false)
}

But I feel this is not correct way since the navigation stack keeps on adding. Instead I would like to clear the navigation stack and show the first tabBarController in the stack. How can I solve this?

like image 673
Kushal Shrestha Avatar asked May 20 '26 06:05

Kushal Shrestha


1 Answers

make your first viewController your root view controller then on button click

self.navigationController?.popToRootViewController(animated: true)
like image 84
Abhay Singh Avatar answered May 21 '26 20:05

Abhay Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!