Blake Loizides
Blake Loizides

Reputation: 985

Programmatically change TabBar order Objective C

I have a Storyboard Xcode project, and I'm battling to place the order of the tabs in the Tabbar. Which has 4 tabs.

CurrentLocationViewController,  LocationsViewController, MapViewController and AnimalsViewController

I have an Sqlite Database that connects 3 of my Tabs in my project. location, tab and map.

Then I have a Tab which is completely separate to the other 3 tabs in my project.

I want AnimalsViewController or Animals tab to be my first tab in my app. I can add it as my 4th tabbar item no problem but when I change the order to be first in my interface builder, my app crashes.

I can change the order of tag, location and map easily but when I introduce Animal tab at the beginning it crashes.

The Tag, Location and Map tabs are interconnected and the Animals tab is completely separate tab from it.

I know I have to reference the tab in my code below that lies in my AppDelegate and include the AnimalsViewController.h but I have tried to no avail.

Below is my code, pics and error log.

  2013-04-08 16:38:08.075 ShotPlacementGuide[5077:c07] -[AnimalsViewController  viewControllers]: unrecognized selector sent to instance 0xae8dc90
  2013-04-08 16:38:08.077 ShotPlacementGuide[5077:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AnimalsViewController viewControllers]: unrecognized selector sent to instance 0xae8dc90'
  *** First throw call stack:
  (0x24a9012 0x1c5ce7e 0x25344bd 0x2498bbc 0x249894e 0x2443 0xb01157 0xb01747 0xb0294b 0xb13cb5 0xb14beb 0xb06698 0x1fc9df9 0x1fc9ad0 0x241ebf5 0x241e962 0x244fbb6 0x244ef44 0x244ee1b 0xb0217a 0xb03ffc 0x222d 0x2155)
  libc++abi.dylib: terminate called throwing an exception
  (lldb) 

Here is my code:

   UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;

    UINavigationController *navigationController = (UINavigationController *) [[tabBarController viewControllers] objectAtIndex:0];

    CurrentLocationViewController *currentLocationViewController = (CurrentLocationViewController *) [[navigationController viewControllers] objectAtIndex:0];
    currentLocationViewController.managedObjectContext = self.managedObjectContext;

    navigationController = (UINavigationController *) [[tabBarController viewControllers] objectAtIndex:1];

    LocationsViewController *locationsViewController = (LocationsViewController *) [[navigationController viewControllers] objectAtIndex:0];
    locationsViewController.managedObjectContext = self.managedObjectContext;

    MapViewController *mapViewController = (MapViewController *) [[tabBarController viewControllers] objectAtIndex:2];

    mapViewController.managedObjectContext = self.managedObjectContext;

enter image description here

enter image description here

Upvotes: 2

Views: 704

Answers (1)

Felix
Felix

Reputation: 35394

In order to exchange the first and fourth view controller in your tab bar controller use this code:

UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;    
NSMutableArray* viewControllers = [[tabBarController viewControllers] mutableCopy];    
[viewControllers exchangeObjectAtIndex:0 withObjectAtIndex:3];    
[tabBarController setViewControllers:viewControllers];

Upvotes: 1

Related Questions