Mayank Purwar
Mayank Purwar

Reputation: 265

Change tab bar in storyboard dynamically

I am trying to work with story board. I am adding tab bar controller and 3 viewcontrollers to that. On first view I have a button and on that button click I want to remove third tab so that there will be 2 tab present.

I am using below code to remove last tab

NSMutableArray *viewControllersCopy = [[self.tabBarController viewControllers] mutableCopy];
[viewControllersCopy removeObjectAtIndex:2];
NSArray *modifiedViewControllers = [[NSArray alloc] initWithArray:viewControllersCopy];
[self.tabBarController setViewControllers:modifiedViewControllers animated:NO];

but it is crashing by giving error

Directly modifying a tab bar managed by a tab bar controller is not allowed

I have seen many links but not able to find any fine answer for it.

Thanks to frin.

Now I have updated my code to work with navigation controller. Previously it was dealing to view controller. I am using below code to change tab bar

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"Main_iPhone"
                                              bundle:nil];
UIViewController* vc1 = [sb instantiateViewControllerWithIdentifier:@"ViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc1];
NSMutableArray *viewControllersCopy = [[self.tabBarController viewControllers] mutableCopy];
[viewControllersCopy replaceObjectAtIndex:0 withObject:nav];
NSArray *modifiedViewControllers = [[NSArray alloc] initWithArray:viewControllersCopy];
[self.tabBarController setViewControllers:modifiedViewControllers animated:NO];

It is working fine but when I am setting tab bar image it is not set.

If I am using

    UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"Main_iPhone"
                                              bundle:nil];
UIViewController* vc1 = [sb instantiateViewControllerWithIdentifier:@"ViewController"];
NSMutableArray *viewControllersCopy = [[self.tabBarController viewControllers] mutableCopy];
[viewControllersCopy replaceObjectAtIndex:0 withObject:vc1];
NSArray *modifiedViewControllers = [[NSArray alloc] initWithArray:viewControllersCopy];
[self.tabBarController setViewControllers:modifiedViewControllers animated:NO];

Then tab bar image is set but navigation is not showing.

Is there any way to get directly navigation controller from the story board or there is any way to show tab bar image?

https://github.com/mayankpurwar/sample is my code link.

Upvotes: 0

Views: 1042

Answers (1)

frin
frin

Reputation: 4536

Your issue is as I expected not with the code you posted in question, but directly modifying the tabbar on UITabBarController, on line 40 in file FirstViewController.m:

[self.tabBarController.tabBar setItems:[NSArray arrayWithObjects:[arr objectAtIndex:0],[arr objectAtIndex:1], nil]];

Comment this line, the code at line #67 will properly remove the last tab. Also the code at line #84 will crash as well if a tab with name "Item" is found.

To find the location of the crash next time, go to Breakpoint Navigator (Cmd + 7 or View>Navigators>Show Breakpoint Navigator), click the plus icon in bottom left corner, Add Exception Breakpoint. A new breakpoint is made automatically. If you run your code with this breakpoint, it will stop at line #40.

enter image description here

Upvotes: 1

Related Questions