NOUSguide / NGTabBarController

A custom TabBarController implementation for iPhone and iPad
Other
263 stars 0 forks source link

Adding tabs during runtime #10

Open cannyboy opened 12 years ago

cannyboy commented 12 years ago

Is there a way to add tabs during runtime? With UITabBarController, I can do something like this

NSArray *existingTabs = self.tabBarController.viewControllers;
NSMutableArray *mut = [[NSMutableArray alloc] initWithArray:existingTabs];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:[NSBundle mainBundle]];
self.extraViewController = [[ExtraViewController alloc] init];
self.extraViewController = [storyboard  instantiateViewControllerWithIdentifier:@"ExtraViewController"];
[mut addObject:self.extraViewController];
[self.tabBarController setViewControllers:mut animated:YES];

However, I can't get this working with NGTabBarController, when I change the last line above to :

[self.tabBarController setViewControllers:mut]; // no animated

.. I get this exception:

-[NSNull addTarget:action:forControlEvents:]: unrecognized selector sent to instance

.. on this line in NGTabTabController, within the method setTabBarItems

[item addTarget:self action:@selector(handleItemPressed:) forControlEvents:UIControlEventTouchDown];
cannyboy commented 12 years ago

I got round the problem by hiding and un-enabling the ng_tabBarItem when the tab bar is initially created:

self.extraViewController.ng_tabBarItem.hidden = YES;
self.extraViewController.ng_tabBarItem.enabled = NO;

.. and then showing / enabling when neccesary.

amsoell commented 12 years ago

Hi @cannyboy , I'm curious to hear more about your method. I have an application with an NGTabBarController, and I need to be able to basically redraw the tabbar on the fly in various instances. Can you give me a little more detail about how you went about adding new tabs after the NGTabBarController was already presented?

cannyboy commented 12 years ago

@amsoell I was not able to add new tabs after it was created. As mentioned above, all I did was hide the ng_tabBarItem when I created the tab bar. When I needed to see it, I just un-hid the item. Since I created the tab bar in my app delegate I first defined a shortcut to my AppDelegate in AppDelegate.h

#define sharedAppDelegate (AppDelegate *) [[UIApplication sharedApplication] delegate]

I also made the vertical tab bar controller a property, as well as the 'hidden' extra view controller

Within AppDelegate.m I had this method:

- (void)showHiddenTab
{
     self.extraViewController.ng_tabBarItem.hidden = NO;
    self.verticalTabBarController.selectedIndex = self.verticalTabBarController.viewControllers.count-1; // switch to last one
}

.. which I could call from any other view controller like so:

#import "AppDelegate.h"
....
// In a buttons action:
[sharedAppDelegate showHiddenTab];
amsoell commented 12 years ago

Thanks, @cannyboy. Not the answer I was hoping for, but I appreciate your time! @myell0w, I would love to know if there is a good / preferred way to update the tabs in an NGTabBarController dynamically.