1

I was working with the switch control, its value sets to default 'off' whenever I navigate from the page or restart the application. How to over come that, so that if pressed on it should stay on or if pressed off it should stay off, until we change manually?

Here is my code where I'm allocating and setting ffram for scrollview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell new";
     listData =[self.tableContents objectForKey:[self.sortedgroups objectAtIndex:[indexPath section]]];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];


    if(indexPath.section==0)
    {  
        switch (indexPath.row) 
        {
            case 0:
            {
                switcher = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
                [switcher addTarget:self action:@selector(switchAction:)
                   forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = switcher;
                switcher.tag = indexPath.row;


                //cell.textLabel.text=@"switcher";

            } 
                break;
            case 1: 
            {


                slide = [[[UISlider alloc] initWithFrame:CGRectMake(0,0, 150, 15)] autorelease];
                [slide addTarget:self action:@selector(slideact:)
                forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = slide;
                // slide.tag = indexPath.row;                
            } 
                break;

}
}
}

This is my method for switch action:

- (void)switchAction:(UISwitch*)sender
{
    NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]);

    if(sender.on)
    {

[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switcher"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    else
    {
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switcher"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
}

-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switcher"]isEqualToString:@"on"])
 {

switcher.on=YES;

    }
else
{
switcher.on=NO;
}

}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
sujay
  • 1,845
  • 9
  • 34
  • 55

3 Answers3

2

Well sameer then you need to save the state of switch. For this you can even use the database or NSUserDefaults. Using NSUserDefaults would be pretty much better.

- (void)switchAction:(UISwitch*)sender
{
    DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));

    if(sender.on)
    {

[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    else
    {
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switch"];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
}

Then in your viewwillAppear method do the following

-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switch"]isEqualToString:@"on"]) {

switch.Ison=YES;

    }
else
{
switch.Ison=NO;
}
}
Sabby
  • 2,586
  • 2
  • 27
  • 41
  • i tried your code, thanks for your time but as i am using a table view , when i turn the switch on and if i scroll the table , the switch button is automatically getting set to off. can u help me out – sujay May 21 '11 at 09:47
0

You should have a value to save the switch control status may on user preferences. When you navigate to this view you should check for this value.

Boolean status=YES;   // You should read this value from User Preference for example it represent the last status set to the switch control.
[switchView setOn:status animated:NO];

Refer to the following question for reading and writing to User Preferences: iphone-reading-and-saving-preferences

Community
  • 1
  • 1
Sarah
  • 1,595
  • 3
  • 25
  • 34
0

You simply use the method to set the switch on and off like this

- (void)switchAction:(UISwitch*)sender
{
    DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));

    if(sender.on)
    {

    }
    else
    {

    }
}

And if you want to set it on default then you just need to set it on in your application say in the implementation file in viewwillappear or at the top of your file after implementaion as

switch.on=YES;
Sabby
  • 2,586
  • 2
  • 27
  • 41
  • if i set switch.on=yes , it will always show on. i want the selected one to show up when i close and restart the application. – sujay May 21 '11 at 08:55
  • 1
    Oh what do you mean, you want the last state of your switch to show up,mean if you have slected it off and you quits the app then again you restart then the state should be off....Well Sameer then you should ask the question in proper way the question is not proper.or you should ask the second question.See my answer below.And you should accept and vote the answer if they help you ,doing so you will get answers otherwise no one give you ansers – Sabby May 21 '11 at 09:00
  • 1
    Sameer see the answer below and do't forget to accept the answer as by by clicking the checkmark..... – Sabby May 21 '11 at 09:08