How to get the value of a UISwitch?

Eristikos picture Eristikos · Feb 9, 2012 · Viewed 30.1k times · Source

I am a newbie iOS programmer and I have a problem.

I currently work on iOS Core Data and my problem is that I want to insert data into a boolean attribute to a database by taking the value of a UISwitch.

The problem is that i don't know what it the method i have to call (e.g .text does the same thing but for UITextField). I have done a small google search but no results. Here is some code:

[newContact setValue:howMany.text forKey:@"quantity"]; 
[newContact setValue:important.??? forKey:@"important"]; 

howmany is a textfield, important is a UISwitch

Answer

Joel Kravets picture Joel Kravets · Feb 9, 2012

To save it

[newContact setObject:[NSNumber numberWithBool:important.on] forKey:@"important"]; 

To retrieve it

BOOL on = [[newContact objectForKey:@"important"] boolValue];