Handling The UISwitch ValueChanged Event To Determine State In MonoTouch

Posting this to help anyone that was stuck trying to determine why the UISwitch State value always returned 0.

The mistake I was making when handling the ValueChanged Event was this (my IBOutlet is switchPrivateOnOff):

switchPrivateOnOff.ValueChanged += delegate {
switchPublicWarning.Text = string.Empty;
switchPublicWarning.Text = switchPrivateOnOff.State.ToString();   // attempt to dump toggle value to UILabel
};

.State always returned 0. It wasn’t until I looked at the apple documentation for UISwitch that I realized I was doing it wrong. I needed to be checking the “On” property of my UISwitch, not the “State”.

switchPrivateOnOff.ValueChanged += delegate {
switchPublicWarning.Text = string.Empty;
switchPublicWarning.Text = switchPrivateOnOff.On.ToString(); // attempt to dump toggle value to UILabel <– this works!
};

Now when I toggle the switch, my app handles the event and will supply the right state!