简体   繁体   English

将 NSUserDefaults 与多个 xib 一起使用

[英]Using NSUserDefaults with multiple xibs

I have been following a tutorial on creating an iPhone app with multiple xibs.我一直在关注有关创建具有多个 xib 的 iPhone 应用程序的教程。 The tutorial describes putting all the code in the View file, rather than the View Controller.本教程描述了将所有代码放在 View 文件中,而不是 View 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.我现在正在考虑使用 NSDefaults 从我的应用程序的选项屏幕中保存设置,然后在用户返回选项屏幕时再次显示它。 The problem is though, the setting is not getting loaded back in.问题是,设置没有重新加载。

In my appdelegate.h file i have got:在我的 appdelegate.h 文件中,我得到了:

NSInteger gMaxDistance;
@property (nonatomic) NSInteger gMaxDistance;

In my appdelegate.m file i have got:在我的 appdelegate.m 文件中,我得到了:

@synthesize gMaxDistance;

In my OptionsView.mi have got:在我的 OptionsView.mi 中有:

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?我如何正确回忆这个值以及代码需要在哪里 go?

If i put it in the OptionsViewController.m viewDidLoad method, the compiler complains that it can't find distanceSlider.如果我把它放在 OptionsViewController.m viewDidLoad 方法中,编译器会抱怨它找不到 distanceSlider。

If i put it in the OptionsView.m file (where all my objects are found) in the initWithFrame method, nothing happens.如果我将它放在 initWithFrame 方法中的 OptionsView.m 文件(在其中找到我的所有对象)中,则不会发生任何事情。

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.您可以从任何 viewController 执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM