如何以编程方式切换uiswitch按钮

问题描述 投票:0回答:4

我以编程方式在tableViewController第3行添加了一个uiswitch。它正在正确显示,我正在NSUserDefaults中保存交换机的状态。我也在比较,取决于NSUserDefaults值和字符串值我试图将我的开关状态改为(ON或OFF)但问题是开关的状态没有改变。

这是我的代码:

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

    static 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];
}

在视图中WillAppear我将字符串与我的NSUserDefaults值进行比较并尝试更改切换状态。它正确地进入断点,但不会将开关状态从ON更改为OFF。

iphone objective-c
4个回答
6
投票

您可以关闭UISwitch设置

[yourSwitch setOn:NO animated:YES]; 

并打开设置

[yourSwitch setOn:YES animated:YES];

0
投票

这没有任何意义:

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];

你创建一个开关,做一些东西,可能是另一个开关,然后释放第一个。试试这个:

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

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

0
投票

尝试在cellForRowAtIndexPath而不是viewWillAppear中编写以下代码:

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

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

0
投票

你可以通过设置简单地切换ON

switchOutlet.on = YES;

并使它成为OFF

switchOutlet.on = NO;
© www.soinside.com 2019 - 2024. All rights reserved.