Phil
Phil

Reputation: 887

shouldSelect method of UITabBarControllerDelegate never invoked

I am using Swift 4 and XCode 9. I am trying to programmatically control navigation in my UITabBarController. According to Apple's documentation, I need to implement the UITabBarControllerDelegate protocol. However, the method I implemented never get called:

import UIKit

class TabBarController: UITabBarController, UITabBarControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        tabBarController?.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController,
            shouldSelect viewController: UIViewController) -> Bool {
        print("Should go here...")
        return true
    }
}

Any idea what I am doing wrong?

Upvotes: 1

Views: 2882

Answers (3)

Leandro Diaz
Leandro Diaz

Reputation: 1

The right method to implement is DidSelect.....

Upvotes: 0

Hades0103
Hades0103

Reputation: 3

To detect the item selected event, you have to override didSelect method.

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    print("Should go here...")
}

Upvotes: 0

rmaddy
rmaddy

Reputation: 318814

Your problem is that you are setting the wrong delegate. Update viewDidLoad to:

override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self // or just "delegate = self"
}

The idea is that you want this tab controller to be its own delegate.

Upvotes: 6

Related Questions