Search code examples
iosswiftuitabbaruitabbaritem

UITabBarItem action not working


I am trying to create a sort of action bar so I restored to using a tab bar because it is clean and is less of a hassle. All 5 of the tabs will be doing some kind of action but with the current code I have it is not doing anything and I am unsure why.

I have the UITabBarDelegate added in my class

class DetailViewController: UIViewController, MKMapViewDelegate, UITabBarDelegate

I have the tab bar set

@IBOutlet weak var optionsBar: UITabBar!

And this is the code I was hoping would print out the statements but is not.

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        if item.tag == 1 {
            print("Tag one")
        } else if item.tag == 2 {
            print("Tag two")
        } else if item.tag == 3 {
            print("Tag three")
        } else if item.tag == 4 {
            print("Tag four")
        } else if item.tag == 5 {
            print("Tag five")
        }
    }

enter image description here

Unlike an array I have the item tags set 1-5 instead of 0-4 but that should not matter, I am very confused. Does anybody know why this is not printing the statements when I click on the UITabBarItems?


Solution

  • You might be forgetting to

    self.optionBar.delegate = self
    

    in viewDidLoad of the ViewController