保存并加载uiswitch

时间:2014-06-25 10:22:42

标签: ios

我试图在iPad上预约表格,我在UISwitch遇到问题,我需要保存其状态,然后将其加载到另一页,我有对文本字段和日期选择器执行此操作但由于某种原因它不适用于UISwitch。我对此非常陌生,所以任何帮助都会受到赞赏。

-(IBAction)savedata:(id)sender; {

    NSString *savestring = _NameofResident.text;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:savestring forKey:@"savedstring"];
    [defaults synchronize];

    NSString *savestring1 = _NameofStudent.text;
    NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
    [defaults1 setObject:savestring1 forKey:@"savedstring1"];
    [defaults synchronize];

    NSString *savestring2 = _AppointmentTime.date;
    NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
    [defaults2 setObject:savestring2 forKey:@"savedstring2"];
    [defaults synchronize];

    NSString *savestring3 = _NextAppointmentTime.date;
    NSUserDefaults *defaults3 = [NSUserDefaults standardUserDefaults];
    [defaults3 setObject:savestring3 forKey:@"savedstring3"];
    [defaults synchronize];

    NSString *savestring4 = _Confirmed.text;
    NSUserDefaults *defaults4 = [NSUserDefaults standardUserDefaults];
    [defaults4 setObject:savestring4 forKey:@"savedstring4"];
    [defaults synchronize];

}


- (IBAction)loaddata:(id)sender; {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *loadstring = [defaults objectForKey:@"savedstring"];
    [_NameofResident setText:loadstring];

    NSString *loadstring1 = [defaults objectForKey:@"savedstring1"];
    [_NameofStudent setText:loadstring1];

    NSString *loadstring2 = [defaults objectForKey:@"savedstring2"];
    [_AppointmentTime setDate:loadstring2];

    NSString *loadstring3 = [defaults objectForKey:@"savedstring3"];
    [_NextAppointmentTime setDate:loadstring3];

    NSString *loadstring4 = [defaults objectForKey:@"savedstring4"];
    [_Confirmed setText:loadstring4];


}

正如您所看到的,我可以为名称和时间加载和保存状态,但我真的不知道如何添加UISwitch来执行此操作。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

要存储UISwitch状态的值,这是一个BOOL,你可以做两件事:

  • 将BOOL存储在NSNumber([NSNumber numberWithBool:myBool])中,并使用默认的setObject:方法保存它。但是,当检索BOOL时,你将不得不做[defaults objectForKey:@“myKey”]。boolValue

  • 使用默认的setBool:forKey方法存储BOOL本身。并使用默认的boolForKey方法检索它。

此外,我已经看到你多次获得默认值并多次同步。这不是必需的,所以我举一个你可以做的例子:

-(IBAction)savedata:(id)sender; {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *savestring = _NameofResident.text;
    [defaults setObject:savestring forKey:@"savedstring"];

    NSString *savestring1 = _NameofStudent.text;
    [defaults setObject:savestring1 forKey:@"savedstring1"];

    NSString *savestring2 = _AppointmentTime.date;
    [defaults setObject:savestring2 forKey:@"savedstring2"];

    NSString *savestring3 = _NextAppointmentTime.date;
    [defaults setObject:savestring3 forKey:@"savedstring3"];

    NSString *savestring4 = _Confirmed.text;
    [defaults setObject:savestring4 forKey:@"savedstring4"];

    BOOL savebool5 = _Switch.isOn;
    [defaults setBool:savebool5 forKey:@"savedbool5"];

    [defaults synchronize];

}


- (IBAction)loaddata:(id)sender; {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *loadstring = [defaults objectForKey:@"savedstring"];
    [_NameofResident setText:loadstring];

    NSString *loadstring1 = [defaults objectForKey:@"savedstring1"];
    [_NameofStudent setText:loadstring1];

    NSString *loadstring2 = [defaults objectForKey:@"savedstring2"];
    [_AppointmentTime setDate:loadstring2];

    NSString *loadstring3 = [defaults objectForKey:@"savedstring3"];
    [_NextAppointmentTime setDate:loadstring3];

    NSString *loadstring4 = [defaults objectForKey:@"savedstring4"];
    [_Confirmed setText:loadstring4];

    BOOL loadBool5 = [defaults boolForKey:@"savedbool5"];
    [_Switch setOn:loadBool5];
}

答案 1 :(得分:0)

您可以通过以下方式解决问题:

//
//  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

快乐的节目!