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.