2

I have a default UITabBarApplication with default views and tabs... etc. I have eight tabs that have the customizable option, so users can reorder tabs.

How do I get the order of the tabs, save that into NSUserDefaults and retrieve it when the app loads back up. I'd like to see some code examples.

Here is where I'm at so far:

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
    NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:8];
    NSArray *savedViews = tabBarController.viewControllers;
    for (UIViewController *theVC in savedViews){
        [vcArray addObject:theVC.title];
    }

    [[NSUserDefaults standardUserDefaults] setObject:vcArray forKey:@"tabLayout"];
}


- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *tabLayout = [defaults arrayForKey:@"tabLayout"];
    NSMutableArray *orderedLayout = [NSMutableArray arrayWithCapacity:8];
    NSArray *defaultOrder = tabBarController.viewControllers;

    for (int i =0; i < 8; i++){
        for (UIViewController *theVC in defaultOrder) {
            if ([theVC.title isEqualToString:[tabLayout objectAtIndex:i]]) {
                [orderedLayout addObject:theVC];
            }
        }
    }

    tabBarController.viewControllers = orderedLayout;
}

This code doesn't seem to work in the simulator. I reorder the tabs and hit done then stop the app, but when I hit build and run again the app reverts to default. Why isn't the code above working?

Thanks.

TheHAWK
  • 99
  • 1
  • 5

1 Answers1

2

One comment:

NSUserDefaults don't always work with the simulator. If you're doing a new build each time, then of course everything gets reset.

Two suggestions:

  1. put an NSLog in the latter method to make sure that it's being called

  2. build the app to a device, move the tabs around, close the app (completely -- not just in background), open the app again, behold what happens.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • Thanks for the reply. I've put NSLog in the didEndCustomizingItems method and from the bottom debugger console, it doesn't get outputted. I built it on my device and still it doesn't save the order. Could it be that the didEndCustomizingItems method isn't being called? – TheHAWK Sep 13 '11 at 18:11
  • @TheHAWK: If the log statement never appears on the console, then the method is never being called. – PengOne Sep 13 '11 at 18:21
  • I've just realized that. :) But, when I add the same NSLog to the applicationDidFinishLaunching method it gets outputting to the console. So why is this method not getting called? – TheHAWK Sep 13 '11 at 18:21
  • @TheHAWK: You must not have set up the `UITabBarDelegate` properly. Check the code where you instantiate the `UITabBar` and be sure that you've set the appDelegate to be the delegate of the tabbar. – PengOne Sep 13 '11 at 18:51
  • I am using the default UITabBarApplication. Can I connect the two in interface builder? If not how would I set the AppDelegate to be the delegate of the tabbar? – TheHAWK Sep 13 '11 at 19:04
  • BTW: Your awesome, you know that? – TheHAWK Sep 13 '11 at 19:12
  • @TheHAWK: I don't use the IB much, so not sure about that. To do it programmatically isn't hard, though. Post your code for the app delegate where you define the tabbar and I may be able to help. All you'll need to do is add the line `[tabbar setDelegate:self];` in the appropriate place within the `AppDelegate.m` file, probably in `– application:didFinishLaunchingWithOptions:`. – PengOne Sep 13 '11 at 20:14
  • This is what I have in my didFinishLaunchingWithOptions now: [self.window addSubview:self.tabBarController.view]; [self.window makeKeyAndVisible]; [self.tabBarController setDelegate:self]; - and it still dont work – TheHAWK Sep 13 '11 at 20:30
  • That snippet you suggested says that tabbar is unidentified. Did you mean: [self.tabBarController setDelegate:self]; ? – TheHAWK Sep 13 '11 at 21:57