0

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.

NeilMortonNet
  • 1,500
  • 4
  • 16
  • 36
  • stepper.value = '[loadNumber doubleValue];' should do the trick. NSUserDefaults are available in all your other view controllers too, since the information is saved on the local file system. just get your information in the other view controller as you do at the moment. – dehlen Dec 20 '13 at 17:40
  • @dehlen Please take a look at http://stackoverflow.com/questions/20708074/ios-uistepper-nsuserdefaults-how-to-manage-them-together-and-use-a-double. I have got that set as you say now, but still having issues. – NeilMortonNet Dec 20 '13 at 17:42

2 Answers2

1

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];
dehlen
  • 7,325
  • 4
  • 43
  • 71
  • See - (IBAction)stepChanged:(id)sender which sets the NSUserDefaults and updates the label upon change. – NeilMortonNet Dec 19 '13 at 15:18
  • Thank you for this. I was not entirely sure how to make this work, and have added amended the code in my question. Any views?! – NeilMortonNet Dec 20 '13 at 14:29
  • Thanks for this. I just shifted the registerDefaults onto the viewController.m file and it worked. So looked back at the appDelegate.m and realised I had placed the two lines AFTER the return YES statement, and thus it was never being called. Thank you. – NeilMortonNet Dec 21 '13 at 15:26
0

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.

Adis
  • 4,512
  • 2
  • 33
  • 40
  • Thank you for this. I was not entirely sure how to make this work, and have added amended the code in my question. Any views?! – NeilMortonNet Dec 20 '13 at 14:30
  • The line should be stepper.value = [loadNumber doubleValue]; – Adis Dec 20 '13 at 17:36
  • Please take a look at http://stackoverflow.com/questions/20708074/ios-uistepper-nsuserdefaults-how-to-manage-them-together-and-use-a-double. I have got that set now, but still having issues. – NeilMortonNet Dec 20 '13 at 17:41