如何获取 UISwitch 的值?
我是一名 iOS 程序员新手,我遇到了一个问题。
我目前正在研究 iOS Core Data,我的问题是我想通过获取 UISwitch
的值将数据插入到数据库的布尔属性中。
问题是我不知道我必须调用什么方法(例如 .text 做同样的事情,但对于 UITextField )。我做了一个小的谷歌搜索,但没有结果。这是一些代码:
[newContact setValue:howMany.text forKey:@"quantity"];
[newContact setValue:important.??? forKey:@"important"];
有多少个文本字段,重要的是 UISwitch
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
保存它
检索它
To save it
To retrieve it
您看过
UISwitch
的文档吗?一般来说,您应该在搜索信息时将文档作为您的第一个调用点,然后转向谷歌,如果您确实找不到您想要的内容,则转向堆栈溢出。您想要
@property(nonatomic, getter=isOn) BOOL on
属性,例如:如果您还没有将 Core Data 设置为使用原语,则可能必须将该布尔值包装在
NSNumber< /代码>:
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:If you haven't got Core Data set to use primitives you may have to wrap that boolean in an
NSNumber
:其他海报是正确的,您需要使用 isOn 方法来获取值,但是这会返回一个 BOOL 值,您不能直接将其传递给 setValue:forKey 因为该方法需要一个对象。
要设置核心数据对象的值,首先将其包装在 NSNumber 中,如下所示:
The other posters are correct that you need to use the isOn method to get the value, however this returns a BOOL value, which you can't pass directly to setValue:forKey because that method expects an object.
To set the value on your core data object, first wrap it in an NSNumber, like this:
我用了
和
I used
And