在 ViewDidLoad
{
NSLog(@"Switch Value %d",delegate.switchvalue);
if(delegate.switchvalue==1)
{
[onoffswitch setOn:YES];
}
else
{
[onoffswitch setOn:NO];
}
}
在设计中,
onoffswitch=[[UISwitch alloc]initWithFrame:(CGRectMake(200, 8, 50, 33))];
[onoffswitch addTarget:self action: @selector(flip:) forControlEvents:UIControlEventValueChanged];
// [onoffswitch setOn:YES];
在切换方法中,
-(IBAction)flip:(id)sender
{
if([sender isOn])
{
[onoffswitch setTag:1];
NSLog(@"%d",[sender tag]);
delegate.switchvalue=[sender tag];
NSLog(@"%d",delegate.switchvalue);
NSLog(@"ON");
NSTimer *BirthTimer=[NSTimer scheduledTimerWithTimeInterval:86400.0 target:self selector:@selector(TimerTapped:) userInfo:nil repeats:YES];
[BirthTimer fire];
}
else
{
NSLog(@"OFF");
}
}
我的问题是,我将开关模式设置为 ON。如果我转到另一个视图,则再次更改为关闭模式。任何想法请帮助我。
感谢您的帮助。
使用 NSUserDefaults
-(IBAction)翻转:(id)发件人
{
[onoffswitch setTag:1];
delegate.switchvalue=[sender tag];
NSLog(@"%d",delegate.switchvalue);
if([sender isOn])
{
NSLog(@"ON");
isAlarm=1;
NSLog(@"%d",isAlarm);
NSUserDefaults *switchdefault=[NSUserDefaults standardUserDefaults];
[[NSUserDefaults standardUserDefaults]synchronize];
[switchdefault setInteger:delegate.switchvalue forKey:@"Switchcontrol"];
[switchdefault setBool:YES forKey:@"SwitchBool"];
[switchdefault synchronize];
NSTimer *BirthTimer=[NSTimer scheduledTimerWithTimeInterval:86400.0 target:self selector:@selector(TimerTapped:) userInfo:nil repeats:YES];
[BirthTimer fire];
}
else
{
NSLog(@"OFF");
isAlarm=0;
NSLog(@"%d",isAlarm);
}
}
在视图中将出现
NSUserDefaults *switchdefault=[NSUserDefaults standardUserDefaults];
if([switchdefault boolForKey:@"SwitchBool"])
{
[onoffswitch setOn:YES animated:YES];
}
else
{
[onoffswitch setOn:YES animated:YES];
}
在另一个视图的 ViewDidLoad 中
NSUserDefaults *switchdefault=[NSUserDefaults standardUserDefaults];
if ([switchdefault boolForKey:@"SwitchBool"])
{
delegate.switchvalue=1;
}
[switchdefault synchronize];