Switch state not linking to Label - ios

For some reason using NSUserDefaults to save the state of my switch and my label don't seem to be working. I can save the state of my switch and load it however unless i manually change the state of the switch the label will not change to on or off.
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
self->switch1.on = ([[standardDefaults stringForKey:#"switchKey"]
isEqualToString:#"On"]) ? (YES) : (NO);
As you can see i added in label.text = #"OFF" when the switch is off but however it still doesn't change.
- (IBAction)switchChanged:(UISwitch *)sender {
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
if (sender.on == 0) {
label.text = #"OFF";
[standardDefaults setObject:#"Off" forKey:#"switchKey"];
} else if (sender.on == 1) {
label.text = #"ON";
[standardDefaults setObject:#"On" forKey:#"switchKey"];
}
[standardDefaults synchronize];
}
Before i wanted to save the switch state i just used this.
-(IBAction) alarmSettings1{
if (switch1.on)
{ label.text = #"ON";}
else { label.text = #"OFF";}
}
I know I'm just doing something simple wrong but can't figure out what.

You should set your boolean like so:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"switchKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
So when you want to set the state of your switch, you would do:
BOOL switchState = [[NSUserDefaults standardUserDefaults] objectForKey:#"switchKey"];
self.switch1.on = switchState;
Also, in your IBAction method, read the state like so:
- (IBAction)switchChanged:(UISwitch *)sender {
label.text = sender.on ? #"ON" : #"OFF";
[[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:#"switchKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

Related

Save switch position after closing app

I wanted to know how to save the position of a switch to close and open the application ?
-(IBAction)switch_poids_passagers:(UISwitch *)sender {
NSString *poids_passagers_total;
if(sender.on) {
poids_passagers_total = #"1";
} else {
poids_passagers_total = #"2";
}
[[NSUserDefaults standardUserDefaults] setObject:poids_passagers_total forKey:#"poids_passagers_total"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSUserDefaults can save and retrieve BOOLS with a pair methods it provides:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:self.myUISwitch.on forKey:#"switchValue"];
// ...
Later on, maybe in viewWillAppear...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL switchValue = [defaults boolForKey:#"switchValue"];
self.myUISwitch.on = switchValue;

how to retain my uitextfield values in viewController when i calling?

I have a main page having 3 textfield in my first view as i navigate to different view and return back to my first view (i.e after i enter all the values i moving to next page).I want my 3 textfield to retain its value when i go back to my main page here my sample code :
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *mobile = MobileNum.text;
[defaults setObject:mobile forKey:self.MobileNum.text];
NSString *amountText = amount.text;
[defaults setObject:amountText forKey:self.amount.text];
[defaults synchronize];
NSString *temp = [defaults objectForKey:self.MobileNum.text];
NSString *temp1 = [defaults objectForKey:self.amount.text];
NSLog(#"%# %#",temp,temp1);
at you viewWillDisappear method store the defaults like.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *mobile = MobileNum.text;
[defaults setObject:mobile forKey:#"mobileNumber"];
NSString *amountText = amount.text;
[defaults setObject:amountText forKey:#"amount"];
[defaults synchronize];
again at your viewWillAppear method retrieve the values like.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults valueForKey:#"mobileNumber"] length]>0){
MobileNum.text=[defaults valueForKey:#"mobileNumber"];
}
else {
MobileNum.text=#"";
}
if ([[defaults valueForKey:#"amount"] length]>0) {
amount.text=[defaults valueForKey:#"amount"];
}
else {
amount.text=#"";
}
it will work.

how to hide Outlet in Xcode and save it from never appearing again?

I want to hide a outlet that is also a button after the action is made, then I need it to be saved so it doesn't appear again.
Ive used in the cation
- (IBAction)giveheart:(id)sender {
[_heartM setHidden:YES];
nLife = [lblLife.text intValue]+1;
[savedStock setObject:[NSNumber numberWithFloat:nLife] forKey:#"life"];
[savedStock writeToFile: path atomically:YES];
}
Now I need to save it to not appear again even when the app is turned off and on again.
How is that done, and what should I do?
Save the state in the NSUserDefaults when button pressed.
- (IBAction)giveheart:(id)sender {
//Save the state in NSUserDefaults here
[[NSUserDefaults standardUserDefaults]setObject:#"Yes" forKey:#"hide"];
[[NSUserDefaults standardUserDefaults]synchronize];
[_heartM setHidden:YES];
nLife = [lblLife.text intValue]+1;
[savedStock setObject:[NSNumber numberWithFloat:nLife] forKey:#"life"];
[savedStock writeToFile: path atomically:YES];
}
in ViewDidLoad method of the viewcontroller
NSString *status = [[NSUserDefaults standardUserDefaults]objectForKey:#"hide"];
if(status != nil && [status isEqualToString:#"Yes"] )
[_heartM setHidden:YES];
You can save the state of the button to NSUserDefaults as follows.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[NSNumber numberWithBool:_heartM.isHidden] forKey:#"buttonState"];
[userDefaults synchronize];
When you view is loaded, you can get the status of button from the defaults
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *boolNum = [userDefaults objectForKey:#"buttonState"];
if(boolNum) {
_heartM.hidden = [boolNum boolValue];
}

How to use UISwitch with NSUserDefault

defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"%#",[defaults objectForKey:#"firsttime"])
if([[defaults objectForKey:#"firsttime"]isEqualToString:#"YES"])
{
UISwitch *onoff = (UISwitch *) sender;
if(onoff.on)
{
NSLog(#"yes on1 facebookswitch");
facebookSwitch.on = YES;
[userDefault setValue:#"true" forKey:#"facebooknotify"];
NSLog(#"on");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
[validate alertCommonWithoutMessage:NSLocalizedString(#"val_message",nil) :NSLocalizedString(#"val_facebook_login",nil) :#"OK"];
facebookSwitch.on = YES;
NSLog(#"yes on2 facebookswitch");
}
else {
NSLog(#"val_facebook_conf");
[validate alertCommonWithoutMessage:NSLocalizedString(#"val_message",nil) :NSLocalizedString(#"val_facebook_conf",nil) :#"OK"];
facebookSwitch.on = NO;
[userDefault setValue:#"false" forKey:#"facebooknotify"];
NSLog(#"yes off1 facebookswitch");
I'm using this method,but sumtimes the response is null.
Try this code:
// add this code in your switch touch event
- (IBAction)YourSwitch:(UISwitch*)sender
{
if (UISwitch.on)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:#"Sound"];
[defaults synchronize];
}
else
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:false forKey:#"Sound"];
[defaults synchronize];
}
}
// add this code in your viewDidload .
BOOL isSound= [[NSUserDefaults standardUserDefaults] objectForKey:#"Sound"];
if (isSound){
_ref_slider.on=TRUE;
} else {
_ref_slider.on=FALSE;
}
i hope this code is useful for you.
As to why it's not working, the most common problem is [userDefault synchronize]
Also, rather than using string boolean values, why not use:
[userDefault setBool:YES forKey:#"facebooknotify"];
and then access by
BOOL notified = [userDefault boolForKey:#"facebooknotify"];

My NSUserDefaults code does not work

When I try to use NSUserDefaults on two different screens, the NSUserDefaults does not apply and there is not an error that comes up. Basically, I want the next level to be available only after the first one is completed.
Here is my code:
View1:
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger level1Complete = [prefs integerForKey:#"levelComplete"];
if (level1Complete == 11) {
button2.hidden = NO;
}
}
View2:
if (number11 == 5) {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:11 forKey:#"levelComplete"];
[prefs synchronize];
}
Please note: the game play itself is view2 and the level select is view1.
[[NSUserDefaults standardUserDefaults] setValue:#"11" forKey:#"levelComplete"];
if you can save it as a value and when you fetch it then convert it into integer.
All The Best!!

Resources