Custom action when clicking on UITabBarController

大憨熊 提交于 2019-12-05 08:01:49

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;
}
  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
         }
    
     }
    

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.

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

You should simply implement the following UITabBarDelegate method:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!