Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of a UISwitch?

Tags:

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

like image 399
Eristikos Avatar asked Feb 09 '12 21:02

Eristikos


People also ask

What is use of UISwitch?

UISwitch(IntPtr) A constructor used when creating managed representations of unmanaged objects; Called by the runtime. UISwitch(NSCoder) A constructor that initializes the object from the data stored in the unarchiver object.

What is a UI switch?

A control that offers a binary choice, such as on/off.


2 Answers

To save it

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

To retrieve it

BOOL on = [[newContact objectForKey:@"important"] boolValue]; 
like image 65
Joel Kravets Avatar answered Oct 16 '22 20:10

Joel Kravets


Have you looked at the docs for UISwitch? Generally ou should make the docs your first point of call when searching for information, then turn to google and then to stack overflow if you really can't find what your after.

You want the @property(nonatomic, getter=isOn) BOOL on property like:

important.isOn 

If you haven't got Core Data set to use primitives you may have to wrap that boolean in an NSNumber:

[NSNumber numberWithBool:important.isOn] 
like image 26
Paul.s Avatar answered Oct 16 '22 20:10

Paul.s