Custom action when clicking on UITabBarController

I have a tab bar controller with four navigation controllers added to it. Navigation controllers appear as tab bar items in the tab bar controller. Now I want to add a fifth button to the tab bar, which does not open another view, but runs some code. I want to display the overlay "sharing menu" when I click on a tab bar item, regardless of which of the four pages the user is on. How can i do this?

+6
source share
4 answers

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

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { if ([viewController == ...your dummy view controller...]) { //Your custom action return NO; } return YES; } 
+18
source
  • In the Storyboard add a UIVIewController and connect it to the tab button that you want to perform with your custom action.

  • Give the UIViewController a unique title. for example, for custom actions. It really doesn't matter, since no one will ever see this title. It is easy to use in the code below to identify that a tab has been clicked.

  • Create a class below and assign it to your UITabBarController in the 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 } } 
+1
source

Krivoblotsky gave the correct answer! I would like to talk more about those who are embarrassed, because for the full implementation there are several more moving parts. Say you have the app below. As soon as you click on the house or profile icon, the corresponding view will be displayed. Say instead of viewing the profile, you want to add your custom transition / behavior.

Application example

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

 @interface ProfileViewController : ViewController <UITabBarControllerDelegate> @end 

2. Access the delegate tabBarcontroller and set it as yourself in your ProfileViewController.m viewDidLoad

 self.tabBarController.delegate = self; 

Essentially, that means hey, do you know the tabBarController delegate? (The guy who is involved in events). I know a guy and let this guy (myself) handle these events. As in English, you are delegating work with other people (you are a delegating entity). The thing that handles the work is DELEGATE.
3. Implementation of user behavior

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

Returning NO says that when ProfileViewController is selected, do not follow the default behavior and do not show it.

Great explanation from delegates

0
source

You should simply implement the following UITabBarDelegate method:

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; 
-1
source

All Articles