Using NSUserDefaults with multiple xibs
I have been following a tutorial on creating an iPhone app with multiple xibs. The tutorial describes putting all the code in the View file, rather than the 开发者_如何学CView Controller.
I am now looking at using NSDefaults to save a setting from my app's options screen, and then display it again when the user goes back into the options screen. The problem is though, the setting is not getting loaded back in.
In my appdelegate.h file i have got:
NSInteger gMaxDistance;
@property (nonatomic) NSInteger gMaxDistance;
In my appdelegate.m file i have got:
@synthesize gMaxDistance;
In my OptionsView.m i have got:
NSUserDefaults *AppSettings = [NSUserDefaults standardUserDefaults];
[AppSettings setInteger:(int)distanceSlider.value forKey:@"MaxDistance"];
[AppSettings synchronize];
How do i correctly recall this value and where does the code need to go?
If i put it in the OptionsViewController.m viewDidLoad method, the compiler complains that it can't find distanceSlider.
If i put it in the OptionsView.m file (where all my objects are found) in the initWithFrame method, nothing happens.
Please help, this has tied me in knots all day!!!
You can get is by doing
NSUserDefaults *appSettings = [NSUserDefaults standardUserDefaults];
int recalledDistance = [appSettings integerForKey@"MaxDistance"];
And you can do it from any viewController.
精彩评论