If you have more than four tabs in an iPhone application’s UITabBar, the user can rearrange them as desired while running. However, to preserve that rearrangement on restart, you need to go to a little effort. Here’s one way to go about it by subclassing UITabBarController, from the Apple discussion boards:
@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
As the hat tip notes, it's not immediately evident why one would use the CFPreferences API for this instead of NSUserDefaults ... but hey, whatever works!
h/t: iphonesdk!
