如何获取 UISwitch 的值?

发布于 2025-01-04 07:55:19 字数 370 浏览 0 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海夜未眠 2025-01-11 07:55:19

保存它

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

检索它

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

To save it

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

To retrieve it

BOOL on = [[newContact objectForKey:@"important"] boolValue];
失去的东西太少 2025-01-11 07:55:19

您看过 UISwitch 的文档吗?一般来说,您应该在搜索信息时将文档作为您的第一个调用点,然后转向谷歌,如果您确实找不到您想要的内容,则转向堆栈溢出。

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

important.isOn

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

[NSNumber numberWithBool:important.isOn]

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]
迎风吟唱 2025-01-11 07:55:19

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

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

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

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:

NSNumber *value = [NSNumber numberWithBool:important.on];
[newContact setValue:value forKey:@"important"];
提笔落墨 2025-01-11 07:55:19

我用了

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

[NSString stringWithFormat:@"%@",(self.allNotificationSwitch.isOn ? @"Yes":@"No")];

I used

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

And

[NSString stringWithFormat:@"%@",(self.allNotificationSwitch.isOn ? @"Yes":@"No")];
故人如初 2025-01-11 07:55:19
[newContact setBool:[NSNumber numberWithBool:important.on] forKey:@"important"];
[newContact setBool:[NSNumber numberWithBool:important.on] forKey:@"important"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文