1

I have a custom TabBarController with a bigger center button in it and when the user press the button it calls a function centerButtonClicked. It works fine, but the problem is that the delegate method shouldSelectViewController is not been called because I set the tabBarItem programmatically. I tried to call the delegate manually but even if I put return NO in the method it still selects the view of tabBarController. All the other tab bar items except for the custom one call the delegate method. I need to return NO to a certain view so I can display an alert view to user. Anyone has a solution to this problem?

This problem was described here but it doesn't work for me: How can I programmatically set selected tab of UITabBarController while also triggering shouldSelectViewController in UITabBarControllerDelegate

In the AppDelegate:

CustomTabBarControllerViewController *tabBarController = [[CustomTabBarControllerViewController alloc] init];

tabBarController.delegate = [TabBarController sharedManager];

The sharedManager is a class method that return a singleton of the controller.

In CustomTabBarControllerViewController:

- (void)centerButtonClicked:(id)sender
{
[self setSelectedIndex:2];

[self performSelector:@selector(doHighlight:) withObject:sender afterDelay:0];

[self.delegate centerButtonPressed:sender];

[self.delegate tabBarController:self shouldSelectViewController:[self.viewControllers objectAtIndex:2]];

}

Updating

In CustomTabBarControllerViewController.h I declare the follow protocol:

@protocol CustomTabBarControllerViewController <NSObject>

- (void)centerButtonPressed: (id)sender;

@end

@interface CustomTabBarControllerViewController : UITabBarController 

@property (nonatomic, strong) IBOutlet UIButton *centerButton;

@property(nonatomic, weak) id<UITabBarControllerDelegate, CustomTabBarControllerViewController> delegate;

@end

This is the delegate that [self.delegate tabBarController:self shouldSelectViewController:[self.viewControllers objectAtIndex:2]]; refers to.

user5818740
  • 43
  • 1
  • 8
  • Where did you add implementation of `shouldSelectViewController`? – SeraZheng Jan 22 '16 at 04:56
  • I implemented the delegate method shouldSelectViewController in TabBarController. It works fine for the normal buttons, but the custom one doesn't call the delegate method. – user5818740 Jan 23 '16 at 01:21
  • I'm sorry for late reply. You assigned delegate selector to `self.delegate` through this code `[self.delegate tabBarController:self shouldSelectViewController:[self.viewControllers objectAtIndex:2]];`. So, what's your `self.delagate`? – SeraZheng Jan 25 '16 at 03:04
  • I'm sorry for later reply too. I set the delegate in the AppDelegate with the follow command: `tabBarController.delegate = [TabBarController sharedManager];` And the method `[self.delegate tabBarController:self shouldSelectViewController:[self.viewControllers objectAtIndex:2]];` is just a call for the delegate method of the UITabBarController with the follow signature `- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController.` I passed `self` to the parameter UITabBarController. Thanks for reply @SeraZheng. – user5818740 Jan 30 '16 at 01:20

0 Answers0