Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:50:56+00:00 2026-05-28T22:50:56+00:00

I’m using NSUserDefaults to keep an object in sync across several UIViewController s that

  • 0

I’m using NSUserDefaults to keep an object in sync across several UIViewControllers that are used in a UITabbarController. To do this, I’m implementing the following

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"ViewControllerX Will Appear");
    [super viewWillAppear:animated];

    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"sharedDictionary"];
    [customObject setDictionary:dict];
}


- (void)viewWillDisappear:(BOOL)animated
{
    NSLog(@"ViewControllerX Will Disappear");
    NSDictionary *dict = [customObject dictionary];
    [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"sharedDictionary"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

Here customObject is an instance of a custom class that has a property dictionary of type NSDictionary. This object may get changed by the visible UIViewController.

The problem I current have is that when the user switches tabs, say from ViewControllerX to ViewControllerY, these methods aren’t getting called in the expected order. I expect to see this in the log:

ViewControllerX Will Disappear
ViewControllerY Will Appear

but instead I see

ViewControllerY Will Appear
ViewControllerX Will Disappear

The result is that the old dictionary is loaded in ViewControllerY, and only after switching tabs again does the new dictionary appear. Is there an easy way around this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:50:57+00:00Added an answer on May 28, 2026 at 10:50 pm

    There’s no guarantee which order these methods are going to be called in, so you can’t rely on any ordering with them. The only guarantee you get is that -viewWillAppear: and -viewWillDisappear: will be called before the view appears or disappears respectively.

    Another way to deal with this might be changing this to a will/did type scenario. So, you save the current state of your object in -viewWillDisappear: and you restore the state (i.e., load your dictionary) in -viewDidAppear:. This will guarantee that the view that is going away saves its dictionary before the view that appears.

    Another approach would be to change the way the custom dictionary is passed between your view controllers and use a delegate object on your application’s UITabBarViewController to deal with syncing these changes to the user defaults. You can integrate this into your app however makes the most sense, but I’ll provide a basic example below along with the changes you’d need to provide to your app (as described in your question):

    To use the example, you need to make these changes (adapt to your coding style):

    • add an NSDictionary to ivar named _sharedDictionary to your application delegate
    • load the dictionary from the user defaults when the app launches
    • declare that your app delegate implements the UITabBarControllerDelegate protocol
    • when your app loads, assign your app delegate as the delegate of your main UITabBarController.
    • change your view controllers to respond to a new property which you can call sharedDictionary
    • you can maintain the rest of your code where in -viewWillAppear you set the value of the view controller’s sharedDictionary property to your custom object and just continue as you did before

    After you’ve done those things, add the following method implementation to your app delegate:

    -(BOOL)tabBarController:(UITabBarController*)tabBarController shouldSelectViewController:(UIViewController*)viewController
    {
      // If you're using ARC, you can remove the retain/autorelease statements
      [_sharedDictionary autorelease];
    
      // In order to avoid a compiler warning here, you should have your view controllers
      // possibly inherit from a parent that defines the sharedDictionary property and cast
      // to that, or have your view controllers implement a protocol that defines the
      // property, and cast to that. As long as your view controllers actually implement
      // the sharedDictionary property, however, everything will work
      _sharedDictionary = [[[tabBarController selectedViewController] sharedDictionary] retain];
    
      // set the shared dictionary on the new view controller -- same casting rules apply
      // as stated above
      [viewController setSharedDictionary:_sharedDictionary];
    
      // save this to user defaults so that if the app stops, it maintains whatever state
      // you're keeping
      dispatch_async(dispatch_get_main_queue(), ^{
        [[NSUserDefaults standardUserDefaults] setObject:_sharedDictionary forKey:@"sharedDictionary"];
        [[NSUserDefaults standardUserDefaults] synchronize];
      });
    
      return YES;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.