1

It seems that UITabBarController should not be subclassed. How would you recommend that I implement a TabBarController in a rotatable DetailView?

Thank you!

AlexR
  • 5,514
  • 9
  • 75
  • 130
  • Why you need to subclass? Why not use standard tab bar. In any cases [Composition](http://en.wikipedia.org/wiki/Object_composition) can substitute inheritance. – beryllium Feb 21 '12 at 08:26

1 Answers1

2

You can add to your controller a delegate to <UITabBarDelegate>,

create a tabBar programmatically

UITabBar * aTabBar;

and fill it with UITabBarItems and then implement this function to handle the touch on a tab to switch views

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
}

This is in brief parts of the code

@interface yourTabsViewController : UIViewController <UITabBarDelegate>
{
    UITabBar * mTabBar;
    NSMutableDictionary * mControllerPerTab;
}
@end

In your implementation :

- (void)viewDidLoad
{
    mControllerPerTab = [[NSMutableDictionary alloc] init];
    [mControllerPerTab setValue:controller forKey:@"aKey"];
        UIImage *bImage = /*icon of tab*/;
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"title" image:bImage tag:/*a tag for your tab*/];
        [tabBarItems addObject:item];
    }

    mTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49/*tabbbar lenght*/ - 44/*navigationbar length if it exists*/, self.view.bounds.size.width ,49)];
    [mTabBar setItems:tabBarItems];
    mTabBar.delegate = self;
    mTabBar.selectedItem = [tabBarItems objectAtIndex:0];
    [self tabBar:mTabBar didSelectItem:[tabBarItems objectAtIndex:0]];
    // Finally, add the tab controller view to the parent view
    [self.view addSubview:mTabBar];
    [super viewDidLoad];
}

Then you add this method to handle the switching of tabs

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    int tag = [item tag];

    /*I'm using the tag to identify wich coltroller to open*/
    UIViewController * controller = [mControllerPerTab objectForKey:[NSString stringWithFormat:@"%d", tag]];
    controller.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height - 49);
    [self.view addSubview:controller.view];
    [self.view addSubview:mTabBar];
    [self.view autoresizesSubviews];
}
Dany Y
  • 6,833
  • 6
  • 46
  • 83
  • I am using storyboard and can't create a tab bar programmatically because I did not subclass the detail view. Can't it be done in Storyboard? – AlexR Feb 21 '12 at 08:40
  • I'm not very advanced in the use of storyboard, but i think what you should do is focus on adding a tabBar to your view and forget the tabBarController, because not only you can't subclass it but it can only be the first controller in your app. so try to add the tabBar and in your code add the delegate and the switching functions – Dany Y Feb 21 '12 at 08:45
  • Ok, would you rather handle the rotation (BarButtonItem addition and removal to toolbar in the tabbarcontroller or in each view controller? – AlexR Feb 21 '12 at 08:55
  • i think(not sure) you should handle the rotation in the tabbarcontroller (the uiviewcontroller inside them have no access to the tabbar buttons). – Dany Y Feb 21 '12 at 09:09
  • @DanyY Can you cite documentation limiting UITabBarController to be the first controller in your app? On the contrary, this quote from the class reference suggests otherwise: To associate a tab bar item with a view controller, create a new instance of the UITabBarItem class, configure it appropriately for the view controller, and assign it to the view controller’s tabBarItem property. – Basil Bourque Jan 20 '13 at 05:56
  • Hi @BasilBourque,if you check the official ios documentation : [link](http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html) , in the second paragraph: Because the UITabBarController class inherits from the UIViewController class, tab bar controllers have their own view that is accessible through the view property. **When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.** – Dany Y Jan 20 '13 at 12:55
  • @DanyY - Thanks for the reference. I did not know that. Funny thing is, I *did* implement a UITabBarController pushed on to a UINavigationController. Works perfectly well, including rotation and the optional toolbar at bottom of nav controller (below the tab bar). Not yet submitted to App Store though. Apple's restriction may be due to their UX policies rather than technical reasons. – Basil Bourque Jan 20 '13 at 23:27