user2636197
user2636197

Reputation: 4122

swift check which tab bar item was clicked

I am using a tab bar controller and I wonder if there is a way to check which tab is being clicked?

If the user clicks on the "account" tab and is not logged in I want to redirect to a full screen modal login screen instead of the account VC.

Upvotes: 1

Views: 5545

Answers (4)

Dan Levy
Dan Levy

Reputation: 4281

The options that others have provided are fine, but I wanted to let you know of another way. In the viewWillAppear, viewDidAppear, or viewDidLoad functions, you can call what you need to segue to a login ViewController

Upvotes: 0

ImWH
ImWH

Reputation: 944

You can do it in your custom UITabBarController or somewhere, and override the 'didSelectItem' function.

import UIKit

class TabbarViewController: UITabBarController {

    override func viewDidLoad() {
}

    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    print("Selected Index :\(self.selectedIndex)");
}

}

Upvotes: 6

Scrungepipes
Scrungepipes

Reputation: 37581

UITabBarDelegate's didSelectItem

Upvotes: 0

estoril
estoril

Reputation: 115

In the scenario you outlined, I would check to see if current user is logged in or not, and if not segue to the appropriate screen of your application.

Upvotes: 0

Related Questions