Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As far as you've asked for some sample code I will simply post here how I dealt with the same task in my app.</p> <p>Quick intro: I was using a NIB file for storing initial <code>UITabBarController</code> state and to differ my tabs one from another I simply defined tag variables for <code>UITabBarItem</code> objects assigned to each <code>UIViewController</code> stuffed in my <code>UITabBarController</code>. To be able to accurately track last selected tab (including the 'More' one) I've implemented following methods for <code>UITabBarControllerDelegate</code> of my <code>UITabBarController</code> and <code>UINavigationControllerDelegate</code> of its moreNavigationController. Here they are:</p> <pre><code>#pragma mark UINavigationControllerDelegate - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [[NSUserDefaults standardUserDefaults] setInteger:mainTabBarController.selectedIndex forKey:@"mainTabBarControllerSelectedIndex"]; } - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [[NSUserDefaults standardUserDefaults] setInteger:mainTabBarController.selectedIndex forKey:@"mainTabBarControllerSelectedIndex"]; } #pragma mark UITabBarControllerDelegate - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { [[NSUserDefaults standardUserDefaults] setInteger:tabBarController.selectedIndex forKey:@"mainTabBarControllerSelectedIndex"]; } </code></pre> <p>And here's the code for saving the tabs order:</p> <pre><code>#pragma mark UITabBarControllerDelegate - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed { int count = mainTabBarController.viewControllers.count; NSMutableArray *savedTabsOrderArray = [[NSMutableArray alloc] initWithCapacity:count]; for (int i = 0; i &lt; count; i ++) { [savedTabsOrderArray addObject:[NSNumber numberWithInt:[[[mainTabBarController.viewControllers objectAtIndex:i] tabBarItem] tag]]]; } [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:savedTabsOrderArray] forKey:@"tabBarTabsOrder"]; [savedTabsOrderArray release]; } </code></pre> <p>As you can see I've been storing the order of tabs' indexes in an array in <code>NSUserDefaults</code>.</p> <p>On app's launch in <code>applicationDidFinishLaunching:</code> method I reordered the <code>UIViewControllers</code> using following code:</p> <pre><code>- (void)applicationDidFinishLaunching:(UIApplication *)application { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; mainTabBarController.delegate = self; int count = mainTabBarController.viewControllers.count; NSArray *savedTabsOrderArray = [[userDefaults arrayForKey:@"tabBarTabsOrder"] retain]; if (savedTabsOrderArray.count == count) { BOOL needsReordering = NO; NSMutableDictionary *tabsOrderDictionary = [[NSMutableDictionary alloc] initWithCapacity:count]; for (int i = 0; i &lt; count; i ++) { NSNumber *tag = [[NSNumber alloc] initWithInt:[[[mainTabBarController.viewControllers objectAtIndex:i] tabBarItem] tag]]; [tabsOrderDictionary setObject:[NSNumber numberWithInt:i] forKey:[tag stringValue]]; if (!needsReordering &amp;&amp; ![(NSNumber *)[savedTabsOrderArray objectAtIndex:i] isEqualToNumber:tag]) { needsReordering = YES; } } if (needsReordering) { NSMutableArray *tabsViewControllers = [[NSMutableArray alloc] initWithCapacity:count]; for (int i = 0; i &lt; count; i ++) { [tabsViewControllers addObject:[mainTabBarController.viewControllers objectAtIndex: [(NSNumber *)[tabsOrderDictionary objectForKey: [(NSNumber *)[savedTabsOrderArray objectAtIndex:i] stringValue]] intValue]]]; } [tabsOrderDictionary release]; mainTabBarController.viewControllers = [NSArray arrayWithArray:tabsViewControllers]; [tabsViewControllers release]; } } [savedTabsOrderArray release]; if ([userDefaults integerForKey:@"mainTabBarControllerSelectedIndex"]) { if ([userDefaults integerForKey:@"mainTabBarControllerSelectedIndex"] == 2147483647) { mainTabBarController.selectedViewController = mainTabBarController.moreNavigationController; } else { mainTabBarController.selectedIndex = [userDefaults integerForKey:@"mainTabBarControllerSelectedIndex"]; } } mainTabBarController.moreNavigationController.delegate = self; [window addSubview:mainTabBarController.view]; } </code></pre> <p>It's quite tricky and may seem strange, but don't forget that my <code>UITabBarController</code> was fully created in a nib file. If you construct it programmatically you may simply do the same but following the saved order.</p> <p>P.S.: and don't forget to synchronize <code>NSUserDefaults</code> when your app terminates.</p> <pre><code>- (void)applicationWillTerminate:(UIApplication *)application { [[NSUserDefaults standardUserDefaults] synchronize]; } </code></pre> <p>I hope this will help. If something is not clear please do comment and ask.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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