在viewDidLoad中UISwitch在IOS
{
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.If我轉到另一個視圖,然後再次關閉模式。 任何想法請幫助我。
感謝您的幫助。
使用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);
}
}
在viewWillAppear中
NSUserDefaults的* switchdefault = [NSUserDefaults的standardUserDefaults ]。
if([switchdefault boolForKey:@"SwitchBool"])
{
[onoffswitch setOn:YES animated:YES];
}
else
{
[onoffswitch setOn:YES animated:YES];
}
在viewDidLoad中AnotherView的
NSUserDefaults *switchdefault=[NSUserDefaults standardUserDefaults];
if ([switchdefault boolForKey:@"SwitchBool"])
{
delegate.switchvalue=1;
}
[switchdefault synchronize];
我試過了,但再次關閉模式 – rajesh
@rajesh - 好的,這裏有什麼問題?你的NSLog爲switchValue打印出正確的值嗎? – katzenhut
我用斷點,但它不能去if語句 – rajesh