anixrud
anixrud

Reputation: 194

UITabBar Event Handling

I'm new to iOS and Swift programming. I used to handle click events in other platforms.

I normally create IBAction for button objects. I wonder is it possible the same thing to handle touch events on TabBar items?

Upvotes: 2

Views: 3054

Answers (3)

nacho c
nacho c

Reputation: 311

Create a control view of the Main view that contains the TabBar and use UITabBarControllerDelegate functions to catch the event when the user tap on one of the items tabs

class TabBarController:  UITabBarController,   UITabBarControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    // UITabBarControllerDelegate
    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        if let navBar = viewController as? NaughtyNavigationController {
            navBar.popToRootViewControllerAnimated(true)
        }
    }
}

Upvotes: 0

pradeepchauhan_pc
pradeepchauhan_pc

Reputation: 384

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
    if(item.tag == 0) {
       // first tab bar code
    }
    else if(item.tag == 1) {
      // second tab bar code
    }
}

Give tag on each tab tag from storyboard utility secton.

Upvotes: 1

zuziaka
zuziaka

Reputation: 575

If you are not using UITabBarController:

See Answer

Otherwise storyboard handle segues for you:

UITabBarController - tutorial (ioscreator.com)

But remember not to use TabBar for other purposes than navigation, user will be confused.

Upvotes: 3

Related Questions