0

I have a list with a few rows, each row containing a switch. I would like to store the boolean values of the list (every time one is clicked) to nsuserdefaults , however im unsure of how to obtain each value. The switch is a UICustomSwitch. Thanks in advance!

PengOne
  • 48,188
  • 17
  • 130
  • 149
mikez
  • 160
  • 1
  • 15

1 Answers1

3

Store booleans with

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:NO forKey:@"myKey"];

Retrieve booleans with

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs boolForKey:@"myKey"];

To integrate this with a swtich, you can do the following;

Store booleans with

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:mySwitch.on forKey:@"myKey"];

Retrieve booleans with

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[mySwitch setOn:[prefs boolForKey:@"myKey"] animated:YES];
PengOne
  • 48,188
  • 17
  • 130
  • 149
  • thanks! how can I attach a listener to each row switch to know when to store the values? – mikez Jul 10 '11 at 05:43
  • 2
    don't forget to use `[prefs synchronize];` after setting the values! – WrightsCS Jul 10 '11 at 06:24
  • Right, but how can I listen to the touch events on each uicustomswitch? – mikez Jul 10 '11 at 18:33
  • 1
    @mikez: See [this answer](http://stackoverflow.com/questions/6631442/synchronizing-plist-across-a-tab-bar-application/6631618#6631618) for how to set up notifications. – PengOne Jul 10 '11 at 19:05