Ryan Hampton Ryan Hampton - 4 months ago 20
Swift Question

Swift: How to return to the initial view controller if there is no navigation controller

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 (source : https://github.com/xmartlabs/XLPagerTabStrip)

Lets call these child view controllers A1, B1 and C1. Within the child view controllers are table views which once tapped segues all the child view controllers to a new view controller (A2). I want to be able to get from A2 back to the first view controller. I have tried add a navigation bar to all 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 initial view controller hosting the child view controllers, 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).


Set the storyboard id of your view controller. Then:

let vc = self.storyboard?.instantiateViewController(withIdentifier: "someID")
self.show(vc, sender: self)