这是 class 1 这里我保存了我的字符串和变量我很确定这个类中的一切都没问题。
-(void)SaveTextBox:(NSString*)string :(NSString*)stringsave
{
NSString *savestring = string;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:stringsave];
[defaults synchronize];
}
- (IBAction)btnSaveTeamNames:(id)sender
{
[self SaveTextBox:tbTeam1.text :@"save1"];
[self SaveTextBox:tbTeam2.text :@"save2"];
[self SaveTextBox:tbTeam3.text :@"save3"];
[self SaveTextBox:tbTeam4.text :@"save4"];
[self SaveTextBox:tbTeam5.text :@"save5"];
[self SaveTextBox:tbTeam6.text :@"save6"];
[self SaveTextBox:tbTeam7.text :@"save7"];
[self SaveTextBox:tbTeam8.text :@"save8"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithInt:TeamAmount] forKey:@"teamAmount"];
}
第2课
这是我尝试加载NSUserDefaults的地方(可能是这个类中的错误)
由于某种原因,当滑块移动时标签不会被更改。我还在我的void上调用了一个viewdidload来加载带有标准保存字符串的标签但是它们也没有加载。
@synthesize lblTeamOne;
@synthesize lblTeamTwo;
@synthesize sliderTeamOneOutlet;
@synthesize sliderTeamTwoOutlet;
-(void)loadTeams:(NSString*)location:(NSString*)lblname
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:location];
lblname = loadstring;
}
-(void)sliderdisplay:(UISlider *)sliderOutlet :(NSString *)lblName
{
if(0 < sliderOutlet.value && 1 >= sliderOutlet.value)
{
[self loadTeams:@"save1" :lblName];
}
if(1 < sliderOutlet.value && 2 >= sliderOutlet.value)
{
[self loadTeams:@"save2" :lblName];
}
if(2 < sliderOutlet.value && 3 >= sliderOutlet.value)
{
[self loadTeams:@"save3" :lblName];
}
if(3 < sliderOutlet.value && 4 >= sliderOutlet.value)
{
[self loadTeams:@"save4" :lblName];
}
if(4 < sliderOutlet.value && 5 >= sliderOutlet.value)
{
[self loadTeams:@"save5" :lblName];
}
if(5 < sliderOutlet.value && 6 >= sliderOutlet.value)
{
[self loadTeams:@"save6" :lblName];
}
if(6 < sliderOutlet.value && 7 >= sliderOutlet.value)
{
[self loadTeams:@"save7" :lblName];
}
if(7 < sliderOutlet.value && 8 >= sliderOutlet.value)
{
[self loadTeams:@"save8" :lblName];
}
}
-(void)loadMaximum
{
int teamAmount = [[[NSUserDefaults standardUserDefaults] objectForKey:@"teamAmount"] intValue];
NSNumber *yourNumber = [NSNumber numberWithInt:teamAmount];
float sliderMaximumDigital = [yourNumber floatValue];
[sliderTeamOneOutlet setMaximumValue:sliderMaximumDigital];
[sliderTeamTwoOutlet setMaximumValue:sliderMaximumDigital];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadTeams:@"save1" :lblTeamOne.text];
[self loadTeams:@"save1" :lblTeamTwo.text];
[self loadMaximum];
}
答案 0 :(得分:1)
您需要将指针传递给UILabel
对象,以便更新:
-(void)loadTeams:(NSString*)location label:(UILabel*)label
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:location];
label.text = loadstring;
}
...
[self loadTeams:@"save1" label:lblTeamOne];