Detect When a Tab Bar Item Is Pressed

Detect when a tab bar item is pressed

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")
}

Detecting when tabBar item is pressed when is page

One option is do the refreshing inside viewWillAppear() method. And the second one is considerably long.

In parent view controller

protocol ParentDelegate {
func refresh()
}

class LandingViewController: UIViewController, UITabBarDelegate {

var delegate: ParentDelegate?
var selectedItem: UITabBarItem!

override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.delegate = self
self.selectedItem = self.tabBar.selectedItem
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "SegueNameForDestinationViewController1" {

if let vc = segue.destinationViewController as? YourDestinationViewController1 {
self.delegate = vc.self
}

} else if segue.identifier == "SegueNameForDestinationViewController2" {

if let vc = segue.destinationViewController as? YourDestinationViewController2 {
self.delegate = vc.self
}

}
}

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

if self.selectedItem == item {
self.delegate?.refresh()
}
self.selectedItem = item
}
}

In each tab view controller,

class TabViewController: UIViewController, ParentDelegate {

func refresh() {
//write your code here
}
}

iphone app - detect which tab bar item was pressed

How to get title of UITabBarItem in the More section?

- (void)tabBarController:(UITabBarController *)tabBarController 
didSelectViewController:(UIViewController *)viewController
{
NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
NSLog(@"controller title: %@", viewController.title);

if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
}

Detect when UITabBar item selected swift

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.

How to detect when pressing down on UIBarButtonItem?

The general idea is, you need to create your own custom UIView, and then pass that into this initialiser.

let customView = MyCustomBarButtonItem()
let barButtonItem = UIBarButtonItem(customView: customView)

As for how you implement the custom view so that you can detect touch downs, you have many choices.

You can either use touchesBegan to detect the touch down, and touchesEnded to detect a "tap" on the bar button item.

class MyCustomBarButtonItem: UIView {
// ...

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// ...
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// ...
}
}

Another way is to subclass UIButton, and add target/action pairs for the .touchDown/.touchUpInside control events.



Related Topics



Leave a reply



Submit