2011-10-14 5 views
0

J'ai ajouté un uiswitch sur ma tableViewViewController rangée 3 par programmation. Il est affiché correctement et je sauve l'état du commutateur dans NSUserDefaults. Je compare aussi, en fonction de la valeur de NSUserDefaults et de la valeur de chaîne que j'essaye de changer l'état de mon commutateur à (ON ou OFF) Mais le problème est l'état du commutateur ne change pas.comment activer le bouton uiswitch par programme

Ceci est mon code:

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

De l'avis WillAppear je compare la chaîne avec ma valeur NSUserDefaults et essayer de changer l'état du commutateur. Il entre correctement dans le point de coupure mais ne change pas l'état de passage de ON à OFF.

Répondre

0

Vous pouvez simplement activer le commutateur en définissant switchOutlet.on = YES; et le désactiver par switchOutlet.on = NO;

+0

j'ai essayé, mais ça ne fonctionne pas – Rani

6

Vous pouvez désactiver UISwitch réglage

[yourSwitch setOn:NO animated:YES]; 

et activer la mise en

[yourSwitch setOn:YES animated:YES]; 
0

Cela ne fait aucun sens:

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

Vous créez un commutateur, faire stuff à, vraisemblablement, un autre commutateur, puis relâchez le premier. Essayez ceci:

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

essayez d'écrire le code suivant dans cellForRowAtIndexPath au lieu de viewWillAppear:

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

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