2
votes

For the app i am building i have all of my Views done in a storyboard and it is using a tabbarcontrolller to switch between the different views. I have one view that i want to programmatically add to this already existing tab bar which already has three items. How can I programmatically add this tab and leave the other tabs be through the storyboard.

More: I did what the you said but nothing happened when i added that it still only has three tabs rather than adding the fourth here is the code i have for the tab bar controller

@interface TheTabBarController ()

@end

@implementation TheTabBarController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIViewController *viewController = [[CrewsViewController alloc]init];
    NSMutableArray *tempArray = [self.tabBarController.viewControllers mutableCopy];
    [tempArray addObject:viewController];
    self.tabBarController.viewControllers = tempArray;
}

@end
3

3 Answers

3
votes

You do this by adding a new controller to the tab bar controller's viewControllers array. So, instantiate the new controller by whatever means is appropriate for how you make the controller. The viewControllers property of a tab bar controller is a immutable array, so you need to create a mutable array from this array, add your controller to it, then set this array to be the tab bar controller's viewControllers array.

UIViewController *newController = [[UIViewController alloc] init]; // or however is appropriate to instantiate your controller
NSMutableArray *tempArray = [self.tabBarController.viewControllers mutableCopy];
[tempArray addObject:newController];
self.tabBarController.viewControllers = tempArray;
1
votes

Try this:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIViewController *view1 = [[UIViewController alloc] init];
    UIViewController *view2 = [[UIViewController alloc] init];
    UIViewController *view3 = [[UIViewController alloc] init];

    NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
    [tabViewControllers addObject:view1];
    [tabViewControllers addObject:view2];
    [tabViewControllers addObject:view3];

    [self setViewControllers:tabViewControllers];
    //can't set this until after its added to the tab bar
    view1.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view1" 
                                    image:[UIImage imageNamed:@"view1"] 
                                      tag:1];
    view2.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view2" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:2];
    view3.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view3" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:3];      
}
0
votes

swift4:

func viewDidLoad() {

super.viewDidLoad()
let view1 = UIViewController()
let view2 = UIViewController()
let view3 = UIViewController()
var tabViewControllers = [AnyHashable]()
tabViewControllers.append(view1)
tabViewControllers.append(view2)
tabViewControllers.append(view3)
if let aControllers = tabViewControllers as? [UIViewController] {
    viewControllers = aControllers
}

//can't set this until after its added to the tab bar

`view1.tabBarItem = UITabBarItem(title: "view1", image: UIImage(named: "view1"), tag: 1)
view2.tabBarItem = UITabBarItem(title: "view2", image: UIImage(named: "view3"), tag: 2)
view3.tabBarItem = UITabBarItem(title: "view3", image: UIImage(named: "view3"), tag: 3)

}