Tab access control is done in AppDelegate, since it owns UITabBarController and UITabBarItems. Just set up delegate handling - and that's the key: either there is a delegate to block tabs or there is none for free access:
@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>Access control can be enabled in any other class/file:
{
UITabBarController *tabBarController;
}
...
- (BOOL)tabBarController:(UITabBarController *)aTabBarController
shouldSelectViewController:(UIViewController *)viewController
{
if (
([aTabBarController.viewControllers objectAtIndex:1] == viewController) ||
([aTabBarController.viewControllers objectAtIndex:2] == viewController)
)
{
// Disable switch to tab 1 and 2
// Check: otherViewController to enable
// Check: SomeViewObject to disable
return NO;
}
else
{
// Tab ok at index 0
return YES;
}
}
- (void)blockTabAccessAccess control can be disabled in any other class/file:
{
MyAppDelegate *app = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
app.tabBarController.delegate = app;
[app SwitchToTab:kTabFirst];
}
- (void)releaseTabAccessSome consider it poor design to access AppDelegate directly from "anywhere" and further more to use it to control application state. My defense are the classic a) it's easy b) it's fast c) it works and d) sometimes central control place is a good thing.
{
MyAppDelegate *app = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
app.tabBarController.delegate = nil;
[app SwitchToTab:kTabSecond];
}
Btw this works only as long as you don't need UITabBarControllerDelegate for any other purpose. You have been warned.
Beside the programmatic side of the suggestion, I wonder if this provides a good user experience.... HIG from Apple says that a users can swap from one view to the other in a Tabbar-based app...
ReplyDeleteThanx for tip, possible review blocker !!!
ReplyDeletePartly it's a design problem: you have to choose where you want to go until you can go there (data in other tabs). What to do when user has not selected...
Ok, have few alternative designs:
- Show "empty" view with info text
- Make a "default" selection for user
Will leave this code snippet here, just in case it's useful at some other time. Thanx, you just saved me a week in AppStore review :)
Suppressing selection of tabbar items is specifically allowed by Apple. However, it is only supposed to be done on a temporary basis.
ReplyDeletehttp://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html#//apple_ref/doc/uid/TP40007457-CH102-SW1