Question

Is there a way to programmatically call a switch that changes to on/off on its own based off an external device reading voltage levels and sending data to the iPod Touch? (5 volts = switch is on, less than 5 = switch is off)

Was it helpful?

Solution

Considering your comment, this should be what you want:

[yourSwitch addTarget:self action:@selector(switchStateChanged:) forControlEvents:UIControlEventValueChanged];

And then, the actual method:

-(void)switchStateChanged:(UISwitch*)thisSwitch{
    // code
}

OTHER TIPS

Is there a way to programmatically call a switch that changes to on/off on its own based off an external device reading voltage levels and sending data to the iPod Touch? (5 volts = switch is on, less than 5 = switch is off)

Yes, you can of course set the on property of the switch to either YES or NO. For example, if your view controller has an outlet called theSwitch, it could set the switch thus:

self.theSwitch.on = YES;

That said, you really shouldn't use a standard switch that way. Switches are user controls, not displays. If the user sees a switch, they'll expect to be able to change it, and for the most part they won't expect the switch to change state by itself. It would be much better to create some sort of indicator that looks different so that you don't confuse your users.

Also, I think there's a paragraph in the App Store submission guidelines about not using Apple UI elements in unexpected ways. If you're planning on publishing your app in the app store, you may risk having it rejected on this basis.

One simple solution would be to use a UISwitch, but set the onImage and offImage properties to your own images, so that the switch doesn't look like something the user expects to control.

Update: From your comment, it sounds like you only want to know when the switch changes. The right answer here (IMO) is that your update method shouldn't change the switch directly, but should instead set some value in the data model. The view controller that manages the view containing the switch could then observe the model using key value observing and adjust the switch's value accordingly, as well as call the switch's action.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top