How to prevent the user from selecting a specific TabBarItem
When a user wants to select a specific UITabBar item, I want to first check if he can select it. And if he can't, then show a warning.
There is a delegate method that is raised when the user selects a specific item, but it doesn't work when the item is disabled.
What is the best way to achieve this?
Thanks.
source to share
This is not a good practice for user interface design. If the button is disabled, it should not respond to user interaction. If it's not disabled, it has to do something. The only time it was justified to have a no-disable button that displays an error / warning rather than performing the desired action is if it can only be determined at the last moment that the action cannot be taken.
source to share
You will need to use the delegate method from UITabBarControllerDelegate:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
In this method, make your check to see which user tried to select by looking at UITabBarController's selectedIndex. Alternatively, you can get a link to the UITabBar itself and check its properties:
UITabBar* tabBar = [tabBarController.view viewWithKindOfClass:[UITabBar class]];
If you don't want the tab to be selected than fire your alert and assign an integer value to tabBarController.selectedIndex to change it on that tab.
Caveat: As the poster above points out, this is not a UI design practice that is compatible with other iPhone apps.
source to share