Question

I have the following code below which uses a stepper to set the value of the myLabel. I have it set to store the value to NSUserDefaults.

1) I also need to set the stepper value to the same figure as that which I have put into the label. Can someone advise how to do this please? (I have added a comment to the code where I feel this should be.)

2) I would also like to check before loading the saveString that it is actually set, and if not, set a default value. Could you also advise how to achieve this?

Thanks.

- (void)viewWillAppear:(BOOL)animated
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *loadString = [defaults objectForKey:@"saveString"];
    self.myLabel.text = loadString;
// Need to set the stepper value to that in loadString
}

- (IBAction)stepChanged:(id)sender {
    NSString *saveString = [NSString stringWithFormat:@"%d",
                            [[NSNumber numberWithDouble:[(UIStepper *)sender value]] intValue]];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:saveString forKey:@"saveString"];
    [defaults synchronize];
    self.myLabel.text = saveString;
}

I have updated the code as follows:-

- (void)viewWillAppear:(BOOL)animated
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSNumber *loadNumber = [defaults objectForKey:@"saveNumber"];
    self.myLabel.text = [NSString stringWithFormat:@"%@",loadNumber];
    UIStepper *stepper = [[UIStepper alloc] init];
    stepper.value = [NSNumber numberWithDouble:loadNumber];
}

- (IBAction)stepChanged:(id)sender {
    NSNumber *saveNumber = [NSNumber numberWithDouble:[(UIStepper *)sender value]];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:saveNumber forKey:@"saveNumber"];
    [defaults synchronize];
    self.myLabel.text = [NSString stringWithFormat:@"%@",saveNumber];
}

However, the line 'stepper.value = [NSNumber numberWithDouble:loadNumber];' errors with the error 'Sending 'NSNumber *__strong" to a parameter of incompatible type 'double''. I think the stepper will only accept a double, but am unsure what I am doing wrong to set it.

What I need to achieve is the following:- 1) Have a default value set in NSUserDefaults for saveNumber, which will be used until a variation is made by the user. 2) on load or view appearing, use saveNumber to set the value of both stepper.value and myLabel. 3) ensure that when changed with the stepper, the value is updated in myLabel, and NSUserDefaults saveNumber is also updated.

Can anyone advise where I am going wrong, and help correct my code please?

Further, something I just realised as I am typing this is that in the final app, I will have this setting on a different viewController to the one that will use the NSUserDefaults savedNumber value. With this in mind, can you also let me know how to ensure that savedNumber is available not only to this viewController, but any other one I require the values on?

Many thanks.

Was it helpful?

Solution

Regarding question 1: Just make one instance variable int stepper; (in your .h) then in viewWillAppear set this variable as you do at the moment; and set

stepper = [defaults integerForKey:@"stepper"];

In your IBAction just call

stepper++;
self.myLabel.text = [NSString stringWithFormat:@"%i",stepper];
and save it in your NSUserDefaults.

Regarding question 2: NSUserDefaults offers the possibility to register defaults for certain keys, have a look at this:

NSDictionary *defaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"test", @"key",
                                nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];

OTHER TIPS

If there is nothing set in the NSUserDefaults, the objectForKey method will return nil, so your loadString will be nil.

Now, you could convert the value you saved from a string into a number, something like [loadString floatValue];, but a probably better way would be to store an NSNumber into the userDefaults with the correct value, and recreate your label text with stringWithFormat method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top