Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I will explain how to do this programmatically. NOTE: This is using ARC, so you may have to insert retain/release calls as needed.</p> <p>You use the <code>tag</code> property of the <code>UITabBarItem</code> for sorting. For every <code>UIViewController</code> that you are adding to the <code>UITabBarController</code>, make sure that each has a unique <code>tag</code>.</p> <pre><code>- (id)init { self = [super init]; if (self) { self.tabBarItem.tag = 0; self.tabBarItem.image = &lt;image&gt;; self.tabBarItem.title = &lt;title&gt;; } return self; } </code></pre> <p>Presumably you would just use their default sorting order for their tags, so whatever you have as your original first view controller would be 0, followed by 1, 2, 3, etc.</p> <p>Set up your UIViewControllers in the AppDelegate's <code>didFinishLaunchingWithOptions</code> as you normally would, making sure that you are instantiating them in their "default order". As you do so, add them to an instance of a NSMutableArray.</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.tabBarController = [[UITabBarController alloc] init]; self.tabBarController.delegate = self; NSMutableArray *unsortedControllers = [NSMutableArray array]; UIViewController *viewOne = [[UIViewController alloc] init]; [unsortedControllers addObject:viewOne]; UIViewController *viewTwo = [[UIViewController alloc] init]; [unsortedControllers addObject:viewTwo]; ... </code></pre> <p>After they are all instantiated and added to the array, you will check to see if the user has customized their order by querying <code>NSUserDefaults</code>. In the defaults, you will store an array of the user's customized tab bar order. This will be an array of NSNumbers (how this is created is explain in the last code snippet). Use these to create a new "sorted" array of view controllers and pass that to the tab bar controller. If they haven't customized the order, the default will return nil and you can simply used the unsorted array.</p> <pre><code> ... NSArray *tabBarOrder = [[NSUserDefaults standardUserDefaults] arrayForKey:@"tabBarOrder"]; if (tabBarOrder) { NSMutableArray *sortedControllers = [NSMutableArray array]; for (NSNumber *sortNumber in tabBarOrder) { [sortedControllers addObject:[unsortedControllers objectAtIndex:[sortNumber intValue]]]; } self.tabBarController.viewControllers = sortedControllers; } else { self.tabBarController.viewControllers = unsortedControllers; } [self.window setRootViewController:self.tabBarController]; [self.window makeKeyAndVisible]; return YES; } </code></pre> <p>To create to customized sort order, use the UITabBarController's delegate method:</p> <pre><code>- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed { NSMutableArray *tabOrderArray = [[NSMutableArray alloc] init]; for (UIViewController *vc in self.tabBarController.viewControllers) { [tabOrderArray addObject:[NSNumber numberWithInt:[[vc tabBarItem] tag]]]; } [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:tabOrderArray] forKey:@"tabBarOrder"]; [[NSUserDefaults standardUserDefaults] synchronize]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload