So, you have :
- the initial view, where you created the buttons button1 and button2
- the tabBarController view.
- 2 "child" views associated
If you want to access to a subview directly (child 1 from button1, child2 from button2)
- create a segue from the initialView to the TabBarController view (from button1 to the tabBarViewController by control-drag)
- define the identifier for the segue to the tab : "SegueToTabBar"
- give a tag to each button : 0 for button1, 1 for button2 (they will reference directly the tabBarItem you want to select)
- define an IBAction, used by both buttons (not needed in fact for button1, because it will segue directly):
@IBAction func theAction(_ sender: UIButton) {
performSegue(withIdentifier: "SegueToTabBar", sender: sender)
}
NOTE: to have all buttons behave identically, you could also create the segue from a hidden button, not from button1:
- create a button in the initialView
- build the Segue to the TabBarView from this button (you have of course no more segue from button1 to TabBarController view)
- name the segue "SegueToTabBar"
- hide this button, which will not be used
- then connect all buttons (1 and 2) to theAction as above
- in the initial view, add the prepare for segue func :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SegueToTabBar" {
if let destVC = segue.destination as? UITabBarController {
destVC.selectedIndex = (sender as! UIButton).tag
}
}
}
the destVC.selectedIndex will lead you directly to the given subview (take care, the first is zero index)
This can easily be extended to a third, fourth child views.
Note: If you tried to segue directly from a button to the view, it would not show the tab bar at the bottom):
- create 2 buttons in the inital view
- control drag from the first button to the first child view
- control drag from the second button to the second child view.