Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when a tab bar item is pressed

I have a root view controller which isn’t set as the custom class for any of my view controllers on my storyboard. Instead, all of my view controllers are subclassing this class like so.

// RootViewController class RootViewController: UIViewController, UITabBarDelegate {       // This is not getting executed on any of the view controllers     func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {         print("ddd")     } }  // Subclassing it  class TopStoriesViewController: RootViewController {  } 

But I seem to be struggling with doing something when a tabbaritem is pressed on the view controller which is subclassing the rootviewcontroller, i.e, the message is not being printed.

like image 906
Tunds Avatar asked Nov 20 '15 23:11

Tunds


2 Answers

You don't want your view controller's base class to be a UITabBarDelegate. If you were to do that, all of your view controller subclasses would be tab bar delegates. What I think you want to do is to extend UITabBarController, something like this:

class MyTabBarController: UITabBarController, UITabBarControllerDelegate { 

then, in that class, override viewDidLoad and in there set the delegate property to self:

self.delegate = self 

Note: This is setting the tab bar controller delegate. The tab bar has it's own delegate (UITabBarDelegate), which the tab bar controller manages, and you are not allow to change.

So, now this class is both a UITabBarDelegate (because UITabBarController implements that protocol), and UITabBarControllerDelegate, and you can override/implement those delegate's methods as desired, such as:

// UITabBarDelegate override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {     print("Selected item") }  // UITabBarControllerDelegate func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {     print("Selected view controller") } 

I'm guessing you're probably more interested in the latter. Check out the documentation to see what each of these delegates provide.

Last thing, in your storyboard (assuming you are using storyboards), set your tab bar controller's class to MyTabBarController in the Identity Inspector, and you're good to go.

Swift 3/4

// UITabBarDelegate override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {     print("Selected item") }  // UITabBarControllerDelegate func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {     print("Selected view controller") } 
like image 116
mbeaty Avatar answered Sep 25 '22 08:09

mbeaty


Doing it this way caused me an error

Changing the delegate of a tab bar managed by a tab bar controller is not allowed

This is what I did and it worked

  1. In you ViewController you inherit UITabBarControllerDelegate
  2. Set delegate in a viewDidLoad
  3. Add a function

Example:

class MyClass: UIViewController, UITabBarControllerDelegate {     func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {         let tabBarIndex = tabBarController.selectedIndex         if tabBarIndex == 0 {             //do your stuff         }    }     override func viewDidLoad() {         super.viewDidLoad()         self.tabBarController?.delegate = self    }  } 
like image 32
Gulz Avatar answered Sep 24 '22 08:09

Gulz