简体   繁体   中英

UISwitch in ios

In ViewDidLoad

{
     NSLog(@"Switch Value %d",delegate.switchvalue);

    if(delegate.switchvalue==1)
    {
        [onoffswitch setOn:YES];
    }
    else
    {
        [onoffswitch setOn:NO];
    }

}

In Designing,

 onoffswitch=[[UISwitch alloc]initWithFrame:(CGRectMake(200, 8, 50, 33))];
    [onoffswitch addTarget:self action: @selector(flip:) forControlEvents:UIControlEventValueChanged];
   // [onoffswitch setOn:YES];

In Switch Method,

-(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");

    }
}

My Problem is,I set the switch mode in ON.If I go to another view,then change again off mode. Any Idea Please Help me.

Thanks to advance for your help.

Using NSUserDefaults

-(IBAction)flip:(id)sender

{

[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);


}

}

In ViewWillappear

NSUserDefaults *switchdefault=[NSUserDefaults standardUserDefaults];

if([switchdefault boolForKey:@"SwitchBool"])
{
    [onoffswitch setOn:YES animated:YES];
}
else
{
    [onoffswitch setOn:YES animated:YES];
}

In ViewDidLoad of AnotherView

NSUserDefaults *switchdefault=[NSUserDefaults standardUserDefaults];

if ([switchdefault boolForKey:@"SwitchBool"])
{
    delegate.switchvalue=1;
}
[switchdefault synchronize];

Use NSSUserDefault to store the status of your switch button and again while adding it to your view retrieve it from the same...

Store 1 once you switch it ON and the 0 for OFF mode.

[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"values"];

and get it back like this,

NSInteger mode = [[NSUserDefaults standardUserDefaults] integerForKey:@"values"];

NOTE : I suggest you to add your switch button in the ViewWillAppear/ViewDidAppear .

If you want the switch to be set to delegate.switchValue every time your view appears, try moving this code from viewDidLoad to viewDidAppear:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if(delegate.switchvalue==1) 
    {
        [onoffswitch setOn:YES];
    }
    else
    {
        [onoffswitch setOn:NO];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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