Custom action when clicking on UITabBarController
Asked Answered
H

4

6

I have a Tab Bar Controller with four navigation controllers added to it. The navigation controllers appear as Tab Bar Items in the Tab Bar Controller. Now I want to add a fifth button to the tab bar, that does not open another view, but triggers some custom code. I want to display an overlaying "share menu" when clicking that Tab Bar Item regardless on which of the four pages the user is. How can I do that?

Hendry answered 14/3, 2014 at 18:29 Comment(0)
C
19

I can suggest to add dummy UIViewController to the last index and handle UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    if ([viewController == ...your dummy view controller...]) {

        //Your custom action

        return NO;
    }

    return YES;
}
Contemn answered 14/3, 2014 at 18:33 Comment(4)
Thank you, that looks great. What would you put there instead of "your dummy view controller"? Can I identify that controller with an ID?Hendry
VC don't have identifier, but there are many ways, to identify it. For ex. you can subclass this dummy VC and name it something like DummyViewController. Than simple check [viewController isKindOfClass:[DummyViewController class]];Contemn
You can also check viewController.tabBarItem.tag in shouldSelectViewController they all have a default of 0. It is settable in storyboard.Hoekstra
Thank you, it looks the right solution, do I need to implement the TabBarController?Superstar
O
1

Krivoblotsky has given the right answer! I'd like to elaborate a little more for anyone who is confused because for the full implementation there are a couple more moving parts. Let's say you have the app below. As it is when you click the home or profile icon the respective view will display. Let's say instead of the profile view to display, you want to add your custom transition / behavior.

Sample application

To do this: 1. Given ProfileViewController class, you want include the UITabBarControllerDelegate in your ProfileViewController

@interface ProfileViewController : ViewController <UITabBarControllerDelegate> @end

2. Access your tabBarcontroller's delegate and set this as yourself in your ProfileViewController.m's viewDidLoad

self.tabBarController.delegate = self;

Essentially what this does is say hey, you know the tabBarController's delegate? (The guy that handles events) I know a guy and let this guy (self) handle those events instead. Like in English, you DELEGATE work to other people (you are the delegating object). The thing that handles the work, is the DELEGATE.
3. Implement the custom needed behavior

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:
    if ([viewController isKindOfClass:[ProfileViewController class]]){
        NSLog(@"It's a profile");
        return NO };
        };
    else{ return YES; }

The NO return says, when ProfileViewController is selected, do not do default behavior and display it's view.

Excellent explanation of delegates

Officiant answered 10/11, 2015 at 6:49 Comment(0)
N
1
  1. In Storyboard, add a UIVIewController and connect it to the tab button you want to perform your custom action.

  2. Give that UIViewController a unique title. e.g. "for custom action". It really doesn't matter, as nobody will ever see that title. It is just for you to use in the code below to identify that tab was tapped.

  3. Create the class below and assign it to your UITabBarController in Storyboard

    class TabBarController: UITabBarController, UITabBarControllerDelegate {
    
        override func viewDidLoad() {
            delegate = self
        }
    
        func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
            if viewController.title == "for custom action" {
               //do your custom actions
               return false
            }
            return true
         }
    
     }
    
Notional answered 3/12, 2015 at 6:32 Comment(0)
U
-1

You should simply implement the following UITabBarDelegate method:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
Ulrikaumeko answered 14/3, 2014 at 18:39 Comment(1)
That is not allowed if the tab bar is handled by a tab bar controllerUsanis

© 2022 - 2024 — McMap. All rights reserved.