SELECT A SPECIFIC VIEW OF TAB BAR CONTROLLER

Hi everyone,


I'm programming my first App with XCode. And I have now a little problem.

My App start with a view controller where I have put some buttons.

And I have after a tab bar controller already done.

But when I link one button of the View Controller to the Tab Bar Controller, I don't succeed in selecting a specific view of the Tab Bar Controller.

My idea is to select button1 and the App will display the View1 of the Tab Bar, and if Button2 is selected, display view2 of tabbar.

I didn't found any tutotrial clearly explain about how to manage this.

Can someone explain me step by step how to fix this issue?


Many thanks.

Regards

Accepted Reply

Great.


Don't forget to close the therad by marking the correct answer.


And good continuation.

Replies

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.

Hi Claude31

Many thanks, you fixed my issue, that works now :-)

Great.


Don't forget to close the therad by marking the correct answer.


And good continuation.