-2

i have 3 switches on my view controller. If the user changes any of these then the 3 settings need to be persisted to a db.

so

switch a = 1, switch b = 0 and switch c = 1

persist to db

switch a = 0, switch b = 1  and switch c = 1

perssit to db

switch a = 0, switch b = 0 and switch c = 0 

persist to db etc

I can trigger one fine with a event but when i try and compare the other ones when the event triggers on one switch i get loads of errors.

I am using an If command and switch.on.

So how can i trap an event when three switches are being amended.

thanks

UPDATE :::

I have done this code : and when i press the button i get the error

    [self.Switch1 addTarget:self action:@selector(flipswitch:) forControlEvents:UIControlEventValueChanged];
    [self.NSwitch2 addTarget:self action:@selector(flipswitch:)forControlEvents:UIControlEventValueChanged];
    [self.Switch3 addTarget:self action:@selector(flipswitch:)forControlEvents:UIControlEventValueChanged];

}


- (IBAction) flipSwitch: (UISwitch *) sender
{

}

and when i change a switch i get

2014-03-05 18:11:44.775 OutTonight[4788:60b] -[SettingsViewController flipswitch:]: unrecognized selector sent to instance 0x15ec9fd0 2014-03-05 18:11:44.777 OutTonight[4788:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SettingsViewController flipswitch:]: unrecognized selector sent to instance 0x15ec9fd0'

All I am trying to do is compare 3 switches and then just persist to the database.

Pawan Rai
  • 3,434
  • 4
  • 32
  • 42

1 Answers1

1

Next time make sure to display what errors you receive, so we can help you more specifically.

To solve your problem, you need to follow those steps.

  1. Create three IBOutlet's in your view controller, one for each switch, if you are using the Storyboard/Interface Builder. If you created them in code, create three instance variables.

  2. Create a method such as:

    - (IBAction) flipSwitch: (UISwitch *) sender
    {
        ....
    }
    
  3. Connect your 3 switches target action's with the following method:

    [switch addTarget: self action: @selector(flipSwitch:) forControlEvents: UIControlEventValueChanged];
    
  4. Implement your flipSwitch: method to access all 3 switches via instance variables you created in step 1 and save their status to the database. You can access the on property of each UISwitch instance to know which are on and off.

For more information on how to use UISwitch see the links below:

And many more examples can be found on Google.

Community
  • 1
  • 1
Legoless
  • 10,942
  • 7
  • 48
  • 68