Showing posts with label UITabBarController. Show all posts
Showing posts with label UITabBarController. Show all posts

Tuesday, October 5, 2010

How to Disable UITabBarItems but Only Temporarily

User is allowed to use only one UITabBar item, until he has done something e.g. made a selection on list. Other tabs are available after that. Access to tabs can be blocked again e.g. if user chooses to delete current "entry".

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>
{
   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;
  }
}
Access control can be enabled in any other class/file:
- (void)blockTabAccess
{
  MyAppDelegate *app = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
  app.tabBarController.delegate = app;
  [app SwitchToTab:kTabFirst];
}
Access control can be disabled in any other class/file:
- (void)releaseTabAccess
{
  MyAppDelegate *app = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
  app.tabBarController.delegate = nil;
  [app SwitchToTab:kTabSecond];
}
Some 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.

Btw this works only as long as you don't need UITabBarControllerDelegate for any other purpose. You have been warned.

Thursday, September 30, 2010

How to Create Default.png

Every released application must have a Default.png file, which is presented during startup. It is supposed to present the default startup view - but empty.


Since all applications are unique (even the thousands of fart apps), there is no single way to create a default.png. However here's two tips, which I use:

1) If your startup view contains a list, create empty list by defining number of sections as 0 (zero):
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // For Default.png creation
    return 0;
}
2) If your application has UITabBar, clear tab titles:
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // For Default.png creation
    for (UITabBarItem *item in self.parentViewController.tabBarController.tabBar.items)
        item.title = @"";
}
In general Default.png should not contain any text strings, since one day you might translate your application to other languages. That day you don't want to flash different language strings during app launch.


...oh, and how to create the actual file? Fix your app to present an "empty screen", build and launch debug version in your handset and take a screenshot in your real device by pressing "Power Button" at top and "The Other Button" at front at the same time. Finally open iPhoto in Mac OSX machine and transfer the pic from iPhone...

Monday, May 3, 2010

How to Create Delayed Tab Change

Not sure, if it's a good idea, but this is how you create "smooth" tab view changes:
#pragma mark - TabBar controller delegate
- (void)tabBarController:(UITabBarController*)tbc
  didSelectViewController:(UIViewController*)newSelection
{
    [UIView beginAnimations:@"myTabFade" context:nil];
    [UIView setAnimationDuration:0.5];
    for (UIViewController* vc in tbc.viewControllers)
        vc.view.alpha = (vc==newSelection) ? 1 : 0;
    [UIView commitAnimations];  
}
User will notice something happened as expected and your app will look either stylish or slow, depending on your graphics, UI design and overall implementation.

Be careful, style is not an easy thing to achieve!