iPhone设置和阅读状态UISwitch
我有一个视图,其中的对象是从数据库设置的,并且同样保存到数据库中。 UITextViews 工作得很好,但我似乎无法找到如何设置 UISwitch 的状态。
我可以在 IB 中更改它,但这不是我需要的。 DB 中的记录具有 0 或 1 布尔值。因此,如果该字段为 true,那么我想将 UISwitch 的状态设置为 ON。
此外,当我保存记录时,我需要能够查看视图上的值,从而在表上设置字段。
感谢您的帮助!
编辑:这是我到目前为止所做的:
.h 文件
@interface UserEdit : UIViewController {
IBOutlet UISwitch *male;
}
@property (nonatomic, retain) IBOutlet UISwitch *male;
.m 文件
@synthesize male;
- (void)viewDidLoad {
[super viewDidLoad];
[male SetOn:NO];
}
应用程序在遇到上面的 SetOn 行时会转储
我还需要不仅能够设置该值,而且还能够读取它
I have a view in which its objects are set from a database and likewise saved to a database. The UITextViews are working great, but I cannot seem to find out how to set the state of a UISwitch.
I can change it in the IB but that isnt what I need. The record in the DB has a 0 or 1 boolean. So if the field is true then I want to set the state of the UISwitch to ON.
Also when I save the record, I need to be able to look at the value on the view, and thus set the field on my table.
thanks for any help!!
EDIT: This is what I have done so far:
.h file
@interface UserEdit : UIViewController {
IBOutlet UISwitch *male;
}
@property (nonatomic, retain) IBOutlet UISwitch *male;
.m file
@synthesize male;
- (void)viewDidLoad {
[super viewDidLoad];
[male SetOn:NO];
}
the app dumps when it hits the SetOn line above
I also need to be able not only set the value, but read it too
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以设置 UISwitch 通过
on
或setOn:animated:
方法,具体取决于您的要求。例如:
[yourUISwitch setOn:YES];
检查UISwitch 类参考 了解更多信息。
更新
根据文档,您可以通过
[yourUISwitch isOn];
读取该值You can set the state of a UISwitch either via the
on
orsetOn:animated:
methods, depending on your requirement.e.g.:
[yourUISwitch setOn:YES];
Check the Setting the Off/On State section of the UISwitch Class Reference for more info.
UPDATE
As per the docs, you can read the value via
[yourUISwitch isOn];
您可以通过以下方式进行操作:
You can do it the following way:
有 swift 版本代码来设置和读取 UISwitch 的状态
There is the swift version code to setting and reading UISwitch's state
为了设置 UISwitch,您必须使用此
[yourSwitch setOn: YESanimated: YES];
为了读取 UISwitch,您需要通过布尔类型的开关状态来检索它
NSLog(@"开关状态:%d", [yourSwitch isOn]);
In order to set a UISwitch you have to use this
[yourSwitch setOn: YES animated: YES];
In order to read the UISwitch you need to retrieve it by the state of the switch which is a boolean type
NSLog(@"Switch state :%d", [yourSwitch isOn]);