0

I'm using Storyboards in xcode with iOS5. I have a TabBarController with 6 tabs. Prior to the TabController a user selects a type of account A oR B, if type B is selected I would like to hide one of the tabs.

I have a subclass of UITabBarController and this piece of code works but its not quite what I want.

if (accountType == 2) {
     [[[[self tabBar] items] objectAtIndex:1] setEnabled:NO];
}

This makes my second tab dark and unusable which is ok, but I really wanted this to work...

[[[[self tabBar] items] objectAtIndex:1] setHidden:YES];

But it causes this error: -[UITabBarItem setHidden:]: unrecognized selector sent to instance 0x856f490 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarItem setHidden:]: unrecognized selector sent to instance 0x856f490'

Is there another way of achieving this?

Andy Davies
  • 4,287
  • 5
  • 24
  • 31

2 Answers2

1

Why not waiting with the initialization of the tabBar viewControllers until you know which type of Account your user selects? To do so use the setViewControllers:animated:method for e.g. as followed:

if (accountType == 1) {
    NSArray* controllersForTabBar = [NSArray arrayWithObjects:myVC1,myVC2,myVC3,myVC4,myVC5,myVC6 nil];
     [[[self tabBar] setViewControllers:controllersForTabBar] animated:YES];
}
if (accountType == 2) {
    NSArray* controllersForTabBar = [NSArray arrayWithObjects:myVC1,myVC2,myVC3,myVC4,myVC5, nil];
     [[[self tabBar] setViewControllers:controllersForTabBar] animated:YES];
}

The apple doc for this method says:

When you assign a new set of view controllers runtime, the tab bar controller removes all of the old view controllers before installing the new ones. When changing the view controllers, the tab bar controller remembers the view controller object that was previously selected and attempts to reselect it. If the selected view controller is no longer present, it attempts to select the view controller at the same index in the array as the previous selection. If that index is invalid, it selects the view controller at index 0.

Regarding your error message: You get that error because the tabBar doesn't implement a method setHidden:.

d.ennis
  • 3,445
  • 2
  • 28
  • 36
1

d.ennis answer pointed me in the right direction. Have to tweak it slightly for ios5 with Storyboards...

// load the storyboard by name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

if (accountType == 1) {
   UIViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:@"First"];
   UIViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"Second"];
} else {
   UIViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:@"First"];
   UIViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"Second"];
   UIViewController *tvc = [storyboard instantiateViewControllerWithIdentifier:@"Third"];

}    

tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;

NSArray *controllersForTabBar = [NSArray arrayWithObjects: fvc, svc, nil];

[tabBarController setViewControllers:controllersForTabBar animated:NO];

[self.view addSubview:tabBarController.view];
Andy Davies
  • 4,287
  • 5
  • 24
  • 31