2011-10-14 26 views
0

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

这是我的代码:

- (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状态。

回答

0

您可以简单地通过设置switchOutlet.on = YES;并通过switchOutlet.on将其设置为OFF。

+0

雅我试过了,但它不工作 – Rani

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]; 
} 
相关问题