繁体   English   中英

如何获得UISwitch的价值?

[英]How to get the value of a UISwitch?

我是一个新手iOS程序员,我有一个问题。

我目前正在研究iOS核心数据,我的问题是我想通过获取UISwitch的值将数据插入到数据库的布尔属性中。

问题是我不知道我必须调用什么方法(例如.text对UITextField做同样的事情)。 我做了一个小谷歌搜索,但没有结果。 这是一些代码:

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

howmany是一个文本域,重要的是UISwitch

保存它

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

要检索它

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

你看过UISwitch的文档了吗? 一般来说,你应该在搜索信息时将文档作为你的第一个调用点,然后转向谷歌,然后如果你真的找不到你想要的东西就堆叠溢出。

你想要@property(nonatomic, getter=isOn) BOOL on属性如:

important.isOn

如果您没有将Core Data设置为使用原语,则可能必须将该布尔值包装在NSNumber

[NSNumber numberWithBool:important.isOn]

其他海报是正确的,您需要使用isOn方法来获取值,但是这会返回一个BOOL值,您无法直接传递给setValue:forKey,因为该方法需要一个对象。

要设置核心数据对象的值,首先将其包装在NSNumber中,如下所示:

NSNumber *value = [NSNumber numberWithBool:important.on];
[newContact setValue:value forKey:@"important"];

我用了

[NSString stringWithFormat:@"%d",(self.allNotificationSwitch.isOn ? 0:1)];

[NSString stringWithFormat:@"%@",(self.allNotificationSwitch.isOn ? @"Yes":@"No")];
[newContact setBool:[NSNumber numberWithBool:important.on] forKey:@"important"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM