How do I save a UISlider value? - iphone

How could I save the value of a UISlider ?I tried this, but it didn't work:
-(IBAction)save {
slider1i = slider1.value;
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setInteger:slider1i forKey:#"slider1key"];
[slider1save synchronize];
}
slider1i is a NSinteger, and slider1 is the UISlider ...
Can you help-me ?

As user 698952 said, UISlider's value property is a float. But i'd instead use setFloat like this:
slider1i = slider1.value;
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setFloat:slider1i forKey:#"slider1key"];
[slider1save synchronize];

slider1.valueis float value so you first need to convert it in NSString or NSNumber inorder to save it ..... that makes it a object
-(IBAction)save {
NSNumber *sliderValue = [NSNumber numberWithFloat:slider1.value];
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setObject:sliderValue forKey:#"slider1key"];
[slider1save synchronize];
}
thanks

Related

Storing Multiple value in MutableArray with different value

I have a button which on click I store the index of the current quotes into an array.
The problem is that it is overwriting the same object. How can I change it with different object of the NSNumber to store in the NSMutableArray?
[someButton setImage:[UIImage imageNamed:#"add_favoritegold.png"] forState:UIControlStateHighlighted];
NSUserDefaults *favoriteQuotes = [NSUserDefaults standardUserDefaults];
NSMutableArray *ListOfIndex= [[NSMutableArray alloc] init];
//Want to change with diffrent objecy
**NSNumber* Wrapped = [NSNumber numberWithInt:index];**
[ListOfIndex addObject:Wrapped];
int xOut = [[ListOfIndex lastObject] intValue];
NSLog(#"%d",xOut);
[favoriteQuotes setObject:ListOfIndex forKey:#"Indexes"];
Try this
- (void)saveFavouriteQuoteIndex:(NSUInteger)index
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *savedQuoteIndexes = [[defaults objectForKey:#"Indexes"]mutableCopy];
if (!savedQuoteIndexes) {
savedQuoteIndexes = [NSMutableArray array];
}
NSNumber *indexNumber = #(index);
if (![savedQuoteIndexes containsObject:indexNumber]) {
[savedQuoteIndexes addObject:indexNumber];
[defaults setObject:savedQuoteIndexes forKey:#"Indexes"];
[defaults synchronize];
}
}
You are saving ListOfIndex with key as Indexes.
So, for the next whatever value, it will replace previous value having key Indexes.
EDIT:
Read the array
NSArray *readArray=favoriteQuotes[#"Indexes"];
Add your new value into this same array.

Restoring value in NSUserDefault

I have stored my initial set up values using NSUserDefault like this...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:is_remember_chkd forKey:remember_me.titleLabel.text]; // Button text as key
[defaults setBool:is_signin_auto_chkd forKey:signin_automatic.titleLabel.text];
[defaults setBool:is_signin_secret_chkd forKey:signin_secret.titleLabel.text];
[defaults synchronize];
And I retrieved as ....
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
is_remember_chkd = [defaults objectForKey:remember_me.titleLabel.text];
is_signin_auto_chkd = [defaults objectForKey:signin_automatic.titleLabel.text];
is_signin_secret_chkd = [defaults objectForKey:signin_secret.titleLabel.text];
But I am not getting the last value, Am I doing anything wrong.
For retrieved the NSUserDefaults Value , replace the objectForKey to boolForKey .
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
is_remember_chkd = [defaults boolForKey:remember_me.titleLabel.text];
is_signin_auto_chkd = [defaults boolForKey:signin_automatic.titleLabel.text];
is_signin_secret_chkd = [defaults boolForKey :signin_secret.titleLabel.text];
This should do the trick for you, extract boolValue from the returned value
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
is_remember_chkd = [[defaults objectForKey:remember_me.titleLabel.text] boolValue];
is_signin_auto_chkd = [[defaults objectForKey:signin_automatic.titleLabel.text] boolValue];
is_signin_secret_chkd = [[defaults objectForKey:signin_secret.titleLabel.text] boolValue];
hope it helps. happy coding :)

NSUserDefaults in NSMutableArray

How to set/make this into an NSMutableArray?
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:1 forKey:#"KeyI1"];
[prefs setInteger:1 forKey:#"KeyJ1"];
[prefs setInteger:1 forKey:#"KeyI2"];
[prefs setInteger:1 forKey:#"KeyJ2"];
Which I can use for an if statement something like this:
NSInteger I1=[prefs integerForKey:#"KeyI1"];
NSInteger I2=[prefs integerForKey:#"KeyI2"]
NSInteger J1=[prefs integerForKey:#"KeyJ1"]
NSInteger J2=[prefs integerForKey:#"KeyJ2"]
if ( I1 == index1 && K1 == index2){
}
if ( I2 == index1 && K2 == index2){
}
You can't use setInteger: for NSUserDefaults (which uses a NSMutableDictionary structure) directly. You should use setObject:forKey: and objectForKey: and add your integers in NSNumber objects or write a NSMutableDictionary category that will handle setInteger:forKey: and integerForKey:.

How to store a float in NSUserDefaults

I want to store a float value into NSUserDefaults.
I also need to check that the float value exists..if not I need to assign some value in it.
and retrieve it...for the above I have the below code but it gives me an error.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:#"HANDWRITING_SIZE_SLIDER"] == YES) {
self.sizeSlider.value = 10.0;
} else {
self.sizeSlider.value = [[NSUserDefaults standardUserDefaults] floatForKey:#"HANDWRITING_SIZE_SLIDER"]];
}
Thanks for any help
Use the NSNumber class for this and store it via the setObject:forKey: method so you can check if it exists.
I'd also suggest the usage of constants as keys:
#define HANDWRITING_SIZE_SLIDER #"HSS"
Your code should be along these lines:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:HANDWRITING_SIZE_SLIDER] == nil) {
//doesn't exist in NSUserDefaults, set to default value...
self.sizeSlider.value = 10.0;
} else {
self.sizeSlider.value = [[defaults objectForKey:HANDWRITING_SIZE_SLIDER] floatValue];
}
Somewhere else in your app, you'd set the value in NSUserDefaults like this:
float sizeSliderValue = ...
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:sizeSliderValue] forKey:HANDWRITING_SIZE_SLIDER];
Try this code:
-(IBAction)sliderAction:(id)sender
{
float sliderValue = [sender floatValue];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:sliderValue] forKey:#"keySliderValue"];
NSLog(#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"keySliderValue"]);
}

have to save UITextfield's value to the NSUserDefault

I am a new comer to the iPhone world, I am having a question,I am having a login page in which 2 levels and associated 2 uitextfield,i have to save both the UITextfield's Values(inputt by user) to the NSUserDefault,and new time when we start application both the UITextfield should be filled with the values we hav input before....i am in bid trouble
code would be appreciated
Thanx in advance
-(void)saveLoginToUserDefaults:(NSString*)loginString
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:loginString forKey:#"Login"];
}
}
-(NSString*)retrieveLoginFromUserDefaults
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:#"Login"];
return val;
}