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.
j'ai essayé, mais ça ne fonctionne pas – Rani