我正在Xcode中制作游戏,其中包括每个级别的评分系统。在这里,我有一些使用委托获得NSString
(passedValue1
)的代码。
代表委托代码:
LevelCompleteLevel1
·H
#import "ViewController.h"
@interface LevelCompleteLevel1 : UIViewController{
IBOutlet UILabel *label;
ViewController *secondviewData;
}
@property (nonatomic, retain)ViewController*secondviewData;
的.m
ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
self.secondviewData = second;
secondviewData.passedValue1 = label.text;
[self presentModalViewController:second animated:YES];
在.m
我将当前viewcontroller
标签中的值发送给另一个view controller
,我发送的值称为passedValue1
我发送了需要显示的值并保存在ViewController
ViewController
·H
@interface ViewController : UIViewController{
IBOutlet UILabel *label;
NSString *passedValue1;
}
@property (nonatomic, retain)NSString *passedValue1;
@end
的.m
首先我...... @synthesize passedValue1;
然后我添加代码以接收viewDidLoad
中的值并在UILabel
- (void)viewDidLoad
{
if(passedValue1){
label.text = passedValue1;
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
[defults setObject:label.text forKey:@"labelkey"];
[defults synchronize];
}
label.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"labelkey"];
}
显示值I后,使用NSUserDefault
将其保存到标签中。但是,一旦我重播了我的游戏并获得了另一个分数值,我想将新的passedValue1
值添加到当前保存的值...
例如:
说我玩我的等级并得到10分。然后保存该值并重播我的等级。然后我想获取保存的值并将其添加到我刚刚得分的值中。所以这就是说我得分的第二个值是20.然后我想让我的代码将它们加在一起,给我一个30的值。
提前致谢。
修改 这是我正在使用的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
if(passedValue1){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject * object = [prefs objectForKey:@"lablekey"];
if(object != nil){
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
[defults setObject:passedValue1 forKey:@"lablekey"];
[defults synchronize];
}
else{
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
NSInteger readScore=[[[NSUserDefaults standardUserDefaults] objectForKey:@"labelkey"] integerValue];
NSInteger newValue=readScore+[passedValue1 integerValue];
[defults setObject:[NSString stringWithFormat:@"%d",newValue] forKey:@"labelkey"];
[defults synchronize];
}
}
label.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"lablekey"];
}
答案 0 :(得分:3)
从userDefaults中读取,添加您当前的分数:
//assuming passedValue1 is your new score
NSInteger readScore=[[NSUserDefaults standardUserDefaults] objectForKey:@"labelkey"] integerValue];
NSInteger newValue=readScore+[passedValue1 integerValue];
然后再保存:
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
[defults setObject:[NSString stringWithFormat:@"%d",newValue] forKey:@"labelkey"];
[defults synchronize];
编辑: * 你在labelKey中输入了一个拼写错误,这就是为什么我将它存储在一个字符串中并使用它,所以即使错误你也可能不会出现任何拼写错误 *
将您的方法更改为:
- (void)viewDidLoad{
[super viewDidLoad];
NSString *key=@"labelKey";
// Do any additional setup after loading the view, typically from a nib
if(passedValue1){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject * object = [prefs objectForKey:key];
if(object != nil){
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
[defults setObject:passedValue1 forKey:key];
[defults synchronize];
}
else{
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
NSInteger readScore=[[[NSUserDefaults standardUserDefaults] objectForKey:key] integerValue];
NSInteger newValue=readScore+[passedValue1 integerValue];
[defults setObject:[NSString stringWithFormat:@"%d",newValue] forKey:key];
[defults synchronize];
}
}
label.text = [[NSUserDefaults standardUserDefaults]objectForKey:key];
}