iPhone设置和阅读状态UISwitch

发布于 2024-11-08 16:42:22 字数 708 浏览 5 评论 0原文

我有一个视图,其中的对象是从数据库设置的,并且同样保存到数据库中。 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 技术交流群。

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

发布评论

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

评论(4

甜宝宝 2024-11-15 16:42:22

您可以设置 UISwitch 通过 onsetOn:animated: 方法,具体取决于您的要求。

例如:[yourUISwitch setOn:YES];

检查UISwitch 类参考 了解更多信息。

更新

根据文档,您可以通过[yourUISwitch isOn];读取该值

You can set the state of a UISwitch either via the on or setOn: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];

无人接听 2024-11-15 16:42:22

您可以通过以下方式进行操作:

//
//  ViewController.m
//


#import "ViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;

- (IBAction)switchChanged:(id)sender;

@end


@implementation ViewController

- (IBAction)switchChanged:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.mySwitch.on forKey:@"SwitchState"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mySwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchState"];

}

@end

You can do it the following way:

//
//  ViewController.m
//


#import "ViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;

- (IBAction)switchChanged:(id)sender;

@end


@implementation ViewController

- (IBAction)switchChanged:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:self.mySwitch.on forKey:@"SwitchState"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mySwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchState"];

}

@end
始终不够爱げ你 2024-11-15 16:42:22

有 swift 版本代码来设置和读取 UISwitch 的状态

    //Change switch state
    self.mSwitchAccept.setOn(true, animated: true)
    //Get the switch state
    let switchState : Bool = self.mSwitchAccept.on

There is the swift version code to setting and reading UISwitch's state

    //Change switch state
    self.mSwitchAccept.setOn(true, animated: true)
    //Get the switch state
    let switchState : Bool = self.mSwitchAccept.on
风铃鹿 2024-11-15 16:42:22

为了设置 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]);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文