Iphone setting and reading state UISwitch

iphoneuiswitch

I have a view in which its objects are set from a database and likewise saved to a database. The UITextViews are working great, but I cannot seem to find out how to set the state of a UISwitch.

I can change it in the IB but that isnt what I need. The record in the DB has a 0 or 1 boolean. So if the field is true then I want to set the state of the UISwitch to ON.

Also when I save the record, I need to be able to look at the value on the view, and thus set the field on my table.

thanks for any help!!

EDIT: This is what I have done so far:

.h file

@interface UserEdit : UIViewController {


    IBOutlet UISwitch *male;

}


@property (nonatomic, retain) IBOutlet UISwitch *male;

.m file

@synthesize male;


- (void)viewDidLoad {
    [super viewDidLoad];

    [male SetOn:NO];


}

the app dumps when it hits the SetOn line above

I also need to be able not only set the value, but read it too

Best Solution

You can set the state of a UISwitch either via the on or setOn:animated: methods, depending on your requirement.

e.g.: [yourUISwitch setOn:YES];

Check the Setting the Off/On State section of the UISwitch Class Reference for more info.

UPDATE

As per the docs, you can read the value via [yourUISwitch isOn];

Related Question