user2257386 user2257386 - 4 months ago 11
iOS Question

how to access the value of a UISwitch

i have created a settingsView and also a view that gives you a choice to be notified if you want at a certain time its a yes or no but i do not know how to access the value of the uiswitch i know instead of trying to get the views object i should try the preference that the state has been saved to but i don't know how i just want to tap my

UISwitch
and then in the settingsView the
UILabel
gets filled but i don't know how to access the value on the
UISwitch
once tapped i have saved it in
NSUserDefaults
in the notifiedView but do not know how to access that value in the settingsView so i can put in the settingsView

if (UISwitch.on)
{
//notify me please
} else {
// leave as is
}


of course that's just a quick layout of how i want it to be though and also where do i put the it in the settingsView viewDidLoad,
viewWillAppear
or both please help im a new be as well so please be kind if i have said things that you would deem not the right syntax thanks

Answer

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 swithState = [[NSUserDefaults standardUserDefaults] boolForKey:@"Sound"];

if (swithState)
{
    _ref_slider.on=true;
}
else
{
    _ref_slider.on=false;
}

i hope this code is useful for you.

Comments