简体   繁体   English

iphone设置和阅读状态UISwitch

[英]iphone setting and reading state UISwitch

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. UITextViews 工作得很好,但我似乎无法找到如何设置 UISwitch 的状态。

I can change it in the IB but that isnt what I need.我可以在 IB 中更改它,但这不是我需要的。 The record in the DB has a 0 or 1 boolean. DB 中的记录具有 0 或 1 布尔值。 So if the field is true then I want to set the state of the UISwitch to ON.因此,如果该字段为真,那么我想将 UISwitch 的状态设置为 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 .h 文件

@interface UserEdit : UIViewController {


    IBOutlet UISwitch *male;

}


@property (nonatomic, retain) IBOutlet UISwitch *male;

.m file .m 文件

@synthesize male;


- (void)viewDidLoad {
    [super viewDidLoad];

    [male SetOn:NO];


}

the app dumps when it hits the SetOn line above应用程序在遇到上面的 SetOn 行时转储

I also need to be able not only set the value, but read it too我还需要不仅能够设置值,而且还需要读取它

You can set the state of a UISwitch either via the on or setOn:animated: methods, depending on your requirement.您可以通过onsetOn:animated:方法设置UISwitch的状态,具体取决于您的要求。

eg: [yourUISwitch setOn:YES];例如: [yourUISwitch setOn:YES];

Check the Setting the Off/On State section of the UISwitch Class Reference for more info.查看UISwitch 类参考的设置关闭/开启状态部分以获取更多信息。

UPDATE更新

As per the docs, you can read the value via [yourUISwitch isOn];根据文档,您可以通过[yourUISwitch isOn];读取[yourUISwitch isOn];

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

There is the swift version code to setting and reading UISwitch's state有设置和读取 UISwitch 状态的 swift 版本代码

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

In order to set a UISwitch you have to use this [yourSwitch setOn: YES animated: YES];为了设置一个 UISwitch,你必须使用这个[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]);为了读取 UISwitch,您需要通过布尔类型NSLog(@"Switch state :%d", [yourSwitch isOn]);来检索它NSLog(@"Switch state :%d", [yourSwitch isOn]);

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

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