7 Replies Latest reply: Jun 21, 2009 1:34 PM by JBMJBM
brakerdizzle Level 1 Level 1 (0 points)
There's probably a really obvious way to do this but I'm a little lost. I want the user to be able to customize the order of the items on a tab bar and then save that order for the next time they use the application. Is there some way I can save this order in an array in a plist file? Thanks for the help!

Macbook, Mac OS X (10.5.4)
  • 1. Re: UITabBar - Saving customized order
    nimnim Level 1 Level 1 (0 points)
    If there is a built-in Apple solution, I don't know it. However, it's not hard to do yourself. I subclassed UITabBarController and added the following code:

    @implementation MemoryTabController

    - (void) awakeFromNib
    {
    \[self setDelegate: self];
    }

    - (void) viewDidLoad
    {
    NSMutableArray *controllers = \[NSMutableArray array];
    NSMutableDictionary *keys = \[NSMutableDictionary dictionary];
    UIViewController *view;
    NSString *title;
    NSArray *order;

    order = (id) CFPreferencesCopyAppValue((CFStringRef) @"MemoryTabControllerViews",
    kCFPreferencesCurrentApplication);
    if (order) {
    for (view in self.viewControllers)
    if (view.tabBarItem.title)
    \[keys setObject: view forKey: view.tabBarItem.title];
    for (title in order)
    \[controllers addObject: \[keys objectForKey: title]];
    for (view in self.viewControllers)
    if (! \[controllers containsObject: view])
    \[controllers addObject: view];
    \[self setViewControllers: controllers];
    \[order release];
    }
    }

    - (void) tabBarController: (UITabBarController *) tabBarController
    didEndCustomizingViewControllers: (NSArray *) viewControllers changed: (BOOL) changed
    {
    NSMutableArray *array = \[NSMutableArray array];
    UIViewController *view;

    for (view in viewControllers)
    if (\[view.tabBarItem.title length])
    \[array addObject: view.tabBarItem.title];
    else {
    NSLog(@"TabBarController cannot save customization unless every item has a title.");
    return;
    }
    CFPreferencesSetAppValue((CFStringRef) @"MemoryTabControllerViews", array,
    kCFPreferencesCurrentApplication);
    CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
    }

    @end

    Message was edited by: nimnim
  • 2. Re: UITabBar - Saving customized order
    RickMaddy Level 4 Level 4 (1,320 points)
    Re-edit your post by wrapping your code like this:


    My code here
    <-- no slash
  • 3. Re: UITabBar - Saving customized order
    nimnim Level 1 Level 1 (0 points)
    As I said:



    If there is a built-in Apple solution, I don't know it. However, it's not hard to do yourself. I subclassed UITabBarController and added the following code:

    @implementation MemoryTabController

    (void) awakeFromNib
    {
    [self setDelegate: self];
    }

    (void) viewDidLoad
    {
    NSMutableArray *controllers = [NSMutableArray array];
    NSMutableDictionary *keys = [NSMutableDictionary dictionary];
    UIViewController *view;
    NSString *title;
    NSArray *order;

    order = (id) CFPreferencesCopyAppValue((CFStringRef) @"MemoryTabControllerViews",
    kCFPreferencesCurrentApplication);
    if (order) {
    for (view in self.viewControllers)
    if (view.tabBarItem.title)
    [keys setObject: view forKey: view.tabBarItem.title];
    for (title in order)
    [controllers addObject: [keys objectForKey: title]];
    for (view in self.viewControllers)
    if (! [controllers containsObject: view])
    [controllers addObject: view];
    [self setViewControllers: controllers];
    [order release];
    }
    }

    (void) tabBarController: (UITabBarController *) tabBarController
    didEndCustomizingViewControllers: (NSArray *) viewControllers changed: (BOOL) changed
    {
    NSMutableArray *array = [NSMutableArray array];
    UIViewController *view;

    for (view in viewControllers)
    if ([view.tabBarItem.title length])
    [array addObject: view.tabBarItem.title];
    else {
    NSLog(@"TabBarController cannot save customization unless every item has a title.");
    return;
    }
    CFPreferencesSetAppValue((CFStringRef) @"MemoryTabControllerViews", array,
    kCFPreferencesCurrentApplication);
    CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
    }

    @end



    I should add that this assumes that every tab bar item has a unique, non-empty title, which is usually a reasonable assumption. You could also tag them some other way.
  • 4. Re: UITabBar - Saving customized order
    ISorinu' Level 1 Level 1 (0 points)
    Hello,
    i'm trying your code to save the order of buttons in the tabbar after editing. i made a class that subclasses UITabBarController<UITabBarControllerDelegate> the viewLoad Method it works but *+tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers: (NSArray *) viewControllers changed: (BOOL) changed+* is never called.. where did you used it? and how? thank you in advance

    ISorinu'
  • 5. Re: UITabBar - Saving customized order
    millz311 Level 1 Level 1 (0 points)
    ISorinu,

    In this case you would set the tabBarController's delegate to be itself in viewDidLoad:

    self.delegate = self;

    Whatever object is set as the delegate will then have that seletor called appropriately.

    Or, even better, do that delegate assignment in Interface Builder if your UITabBarController is created there.
  • 6. Re: UITabBar - Saving customized order
    millz311 Level 1 Level 1 (0 points)
    This method has an issue with System TabBar Items, at least in firmware v2.2.

    System TabBar Items (UITabBarSystemItem*) don't directly provide titles, so if you use those (as you should when possible) they will not work with this. To solve that problem you probably want to use the "tag" property instead of the "title" property. The drawback with that is that tag is not set to something unique by default - you have to go into your XIB (or TabBarItem creation code) and set those to unique numbers. I set mine all > 0 so I can check for 0 as a bad tag value in my code.
  • 7. Re: UITabBar - Saving customized order
    JBMJBM Level 1 Level 1 (0 points)
    I am having an issue with my Tab Bar b/c it has too many tabs and not all of them show up under the customize order b/c they bleed below the bar and there is no scroll function. Is there a way to make the text smaller or scroll?