0

well, I'm trying to connect 3 view controllers with 3 tab bar items in one tab bar, I searched here for the way and I found multiple answers such as: How do you connect a tab bar item to an action? and set up an action with a tab bar item - iphone but i could not figure it out, here is my code:

class TabBar: UIViewController, UITabBarDelegate {
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

        if(item.tag == 1) {
            let us = self.storyboard?.instantiateViewController(withIdentifier: "Th5ViewController") as! Th5ViewController
            self.navigationController?.pushViewController( us, animated: true)

        }
        else if(item.tag == 2) {
            //your code for tab item 2
        }

        else if(item.tag == 3) {
            //your code for tab item 2
        }

    }
}

so, what I missed? I think it a small rational detail. thank you.

Community
  • 1
  • 1
Mzoch
  • 133
  • 1
  • 2
  • 9
  • 1. Why do you call a UIViewController subclass `TabBar`? TabBar is a suitable name for a view, not a controller. 2. What `navigationController` are you referring to? Do you really need to push a new view controller if a tab bar item is selected or just to show a view controller for that tab? – alexburtnik Nov 08 '16 at 22:04
  • @alexburtnik I used the same way I knew it when I set an action to a button with new view controller, and yes I want to push new view controller if the first tab bar item is selected, the others just to show their view controllers. – Mzoch Nov 08 '16 at 22:26
  • If you already connect 3 view controller to 3 tabbar item, you can simply do action when that viewcontroller appear in their `viewWillAppear`, since when press the tabbar item, it will bring you to that view controller anyway? Or you need to do any specific action? – Tj3n Nov 09 '16 at 04:07

0 Answers0