Storyboards, modal popover segues, and programmatically altering UITabBarController tabs

I am going to open a modal popover with a storyboard segue.  The popover has a UITabBarController with 3 tabs.  How do you programmatically remove one of the tabs?  I had a little bit of trouble finding exactly how to do this, so here it is.

First you need to know how to properly remove a tab from UITabBarController.  A quick search brought me to this: Programmatically Removing a Tab from UITabBarController

So now the tricky part is how do you access this UITabBarController that is being opened by a storyboard segue?  This is still the easier part of the problem.  Use the prepareForSegue method on the UIViewController that is going to open the popover.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

}

The part that tripped me up was how to do get access to the UITabBarController.  Several pages on Google results had me believing I had to get the destinationViewController, and then access its rootViewController in order to find the UITabBarController.

It is actually much easier than that.  the destinationViewController IS the UITabBarController.  Here is the code that removes the 3rd tab on the UITabBarController.

https://gist.github.com/4161969

Easy!

Leave a comment