Search code examples
iosuiviewcontrollernsuserdefaultsuiswitch

Creating a UISwitch that can change something in a different view


I am creating a settings view in Xcode and Objective-C and I want it to change where a button leads in a separate view when a UISwitch is switched on.

In more depth the plan is if a switch is on in the settings view then 2 views down the line a button leads to view1 but if that switch is off, it leads to view2. I read something about NSUserDefaults but I wasn't quite sure whether it would work for this.


Solution

  • NSUserDefaults can be an approach for this. You can save the state of the UISwitch as

    [[NSUserDefaults standardUserDefaults] setBool:switch.on forKey:@"switchState"];
    

    then on button click you can check for this value

    BOOL on = [[NSUserDefaults standardUserDefaults] boolForKey:@"switchState"];
    

    and decide which view it will lead to.

    Hope it helps !!!