开发者

How to switch the uiswitch button programmatically

I have added a uiswitch on my tableViewController row 3 programmatically. It is being displayed properly and I am saving the state of the switch in NSUserDefaults. I am also comparing, depending on the NSUserDefaults value and the string value I am trying to change the state of my switch to (either ON or OFF) But the problem is the state of the switch is not getting changed.

This is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    stati开发者_StackOverflowc NSString *CellIdentifier = @"Cell";

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

    switch (indexPath.row) {

        case 2:
            cell.textLabel.text = @"Weather";   
            switchControl = [[UISwitch alloc] initWithFrame:CGRectZero];
            cell.accessoryView = switchControl;

            [self.switchControl setOn:YES animated:NO];
            [self.switchControl addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
            [switchControl release];
            break;


        default:
            break;
    }
    cell.textLabel.text = [settings objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;



    return cell;
}



-(void)switchChanged:(id)sender
{

    app= (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];

        NSString *value;
    userdefaults = [NSUserDefaults standardUserDefaults];
        if(!switchControl.on){
            value = @"OFF";
            [userdefaults setObject:value forKey:@"stateOfSwitch"];
        }
        [userdefaults setObject:value forKey:@"stateOfSwitch"];
    [userdefaults synchronize];

}


- (void)viewWillAppear:(BOOL)animated {
    NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:@"stateOfSwitch"];

    if([_value compare:@"ON"] == NSOrderedSame){
        [self.switchControl setEnabled:YES];
    }
    else {
        [self.switchControl setEnabled:NO];
    }
    [super viewWillAppear:animated];
}

In the view WillAppear I am comparing the string with my NSUserDefaults value and trying to change the state of switch. It properly enters in the breakpoint but does not change the state of switch from ON to OFF.


You can switch off UISwitch setting

[yourSwitch setOn:NO animated:YES]; 

and turn on setting

[yourSwitch setOn:YES animated:YES];


This doesn't make any sense:

switchControl = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchControl;

[self.switchControl setOn:YES animated:NO];
[self.switchControl addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchControl release];

You create a switch, do some stuff to, presumably, another switch, and then release the first one. Try this:

self.switchControl = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchControl;

[self.switchControl setOn:YES animated:NO];
[self.switchControl addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];


Try writing the following code in cellForRowAtIndexPath instead of viewWillAppear:

NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:@"stateOfSwitch"];

if([_value compare:@"ON"] == NSOrderedSame) {
    [self.switchControl setEnabled:YES];
} else {
    [self.switchControl setEnabled:NO];
}


You can simply make the switch ON by setting:

switchOutlet.on = YES;

and make it OFF by:

switchOutlet.on = NO;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜