The problem with NSUserDefaults Opt

After you register the defaults, you still need to do a sync to have
the values actually saved in the default database:

[[NSUserDefaults standardUserDefaults] synchronize];

See if that helps.

Sze Wong
Zerion Consulting
http://www.zerionconsulting.com

On Sep 27, 12:10 am, Tom <sunkunj...@gmail.com> wrote:

> hi, all >   i want to store some status of application to recover application's > status when start next time...for example, i want to display  a > content my last inputed in the searchBar when applicaltion start. i > used NSUserDefaults like this: > - (void)applicationDidFinishLaunching:(UIApplication *)application { >         [aViewController aSearchBar].text = [[NSUserDefaults > standardUserDefaults] objectForKey:@"searchBarText"]; >        ...

> }

> - (void)applicationWillTerminate:(UIApplication *)application
> {
>         NSLog(@"will terminate");

>         NSMutableDictionary *defaultValues = [NSMutableDictionary
> dictionary];
>         NSString *searchBarText = [[NSString alloc] initWithString:
> [aViewController aSearchBar].text];
>         [defaultValues setObject:searchBarText forKey: @"searchBarText"];
>         [[NSUserDefaults standardUserDefaults] registerDefaults:
> defaultValues];
>         NSLog([[NSUserDefaults standardUserDefaults]
> objectForKey:@"searchBarText"]);

>         [[nbWikiUIController nbSearchBar].text writeToFile:@"searchBarText"
> atomically:YES];

> }

> but the content of searchBar is blank when you click the iphone
> simulator's home button and start the appliclation again.how ever, if
> you write some value to a file directly it will work well, like this:

> - (void)applicationDidFinishLaunching:(UIApplication *)application {

>         NSArray *paths =
> NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
> NSUserDomainMask, YES);
>         NSString *documentsDirectory = [paths objectAtIndex:0];
>         NSString *appFile = [documentsDirectory
> stringByAppendingPathComponent:@"searchBarText"];
>         NSString *myString = [NSString stringWithContentsOfFile:appFile];
>         [nbWikiUIController nbSearchBar].text = myString;
>        ...

> }

> - (void)applicationWillTerminate:(UIApplication *)application
> {
>         NSLog(@"wikimate terminate");

>         [[nbWikiUIController nbSearchBar].text writeToFile:@"searchBarText"
> atomically:YES];

> }

> If you do like this, you will get this warning:
> warning: 'writeToFile:atomically:' is deprecated (declared at /var/
> folders/Mv/Mv45MRvrHuKkTR4seo+8kk+++TI/-Caches-/com.apple.Xcode.501/
> CompositeSDKs/iphonesimulator-iPhoneSimulator2.0-
> cwkscxilvbhpyzhgdmxradhnpbjc/System/Library/Frameworks/
> Foundation.framework/Headers/NSString.h:352)

> i don't know why...

> many thanks.

- Hide quoted text -- Show quoted text -