NSUserDefaults
在iphoneos/iossdk中,但我似乎无法让自己的实现正常工作。
然而,
有不同的想法。有人知道为什么我的首选加载方法不断返回吗
(null)
NSLog
?
提前谢谢!:)
对视图控制器.m
(节略)
- (void)viewDidLoad {
// yadda yadda yadda interface setup here...
if ([self retrieveCount] == 0) {
count = 0;
} else {
count = [self retrieveCount];
NSString *temp = [[NSString alloc] initWithFormat:@"%@", count];
[label setText:temp];
[temp release];
}
[super viewDidLoad];
}
int count = 123; //whatever it might be when the user exits the application, set every time the "+" or "-" is tapped
- (void)writeCount {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:count forKey:@"countKey"];
[prefs synchronize];
}
- (int)retrieveCount {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs synchronize];
int loaded = [prefs integerForKey:@"countKey"];
NSLog(@"%@", loaded);
return loaded;
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
[CountrViewController writeCount];
}
(因为我将只从一个键中检索一个值,所以我决定不对该方法进行任何输入。)
如果这是任何混乱的方式,请让我知道!我很乐意澄清。