Tengo un UISwitch
dentro de un UITableViewCell
personalizado (la subclase de la cual llamo RootLabeledSwitchTableCell
).UISwitch dentro de la UITableViewCell personalizada no disponible
La celda contiene un UILabel
y UISwitch
uno al lado del otro.
Tengo un @property
llamada keychainSwitch
que apunta hacia el interruptor dentro de esta celda personalizado:
@interface RootLabeledSwitchTableCell : UITableViewCell {
IBOutlet UILabel *textLabel;
IBOutlet UISwitch *labeledSwitch;
}
@property (nonatomic, retain) IBOutlet UILabel *textLabel;
@property (nonatomic, retain) IBOutlet UISwitch *labeledSwitch;
@end
En mi mesa con vista al delegado, he añadido un selector que se llama si se invierte el estado del interruptor:
- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]];
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
switch (indexPath.section) {
case(kMyFirstSection): {
switch (indexPath.row) {
case(kMyFirstSectionFirstRow) {
[cellOwner loadMyNibFile:@"RootLabeledSwitchTableCell"];
cell = (RootLabeledSwitchTableCell *)cellOwner.cell;
self.keychainSwitch = [(RootLabeledSwitchTableCell *)cell labeledSwitch];
[self.keychainSwitch addTarget:self action:@selector(keychainOptionSwitched) forControlEvents:UIControlEventValueChanged];
break;
}
// ...
}
}
// ...
}
}
Así que este selector funciona correctamente:
- (void) keychainOptionSwitched {
NSLog(@"Switched keychain option from %d to %d", ![self.keychainSwitch isOn], [self.keychainSwitch isOn]);
}
Sin embargo, no puede utilizar el método de la UISwitch
de ejemplo -setOn:animated:
para inicializar su estado inicial:
- (void) updateInterfaceState {
BOOL isFirstTimeRun = [[[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstTimeRunKey] boolValue];
if (isFirstTimeRun) {
[self.keychainSwitch setOn:NO animated:NO];
}
}
De las pruebas, self.keychainSwitch
es nil
que está causando -setOn:animated
no hacer nada, pero todavía se puede accionar el interruptor y la declaración NSLog
imprimirá correctamente los cambios de estado del interruptor, por ejemplo:
[Session started at 2009-08-24 07:04:56 -0700.]
2009-08-24 07:04:58.489 MyApp[29868:20b] keychain switch is: nil
2009-08-24 07:05:00.641 MyApp[29868:20b] Switched keychain option from 1 to 0
2009-08-24 07:05:01.536 MyApp[29868:20b] Switched keychain option from 0 to 1
2009-08-24 07:05:02.928 MyApp[29868:20b] Switched keychain option from 1 to 0
¿hay algo que me falta acerca de la configuración self.keychainSwitch
en el método UITableView
delegado?
Esto no funcionó en mi extremo. Gracias por la sugerencia, sin embargo. –
No funcionó aquí tampoco. Obtuve: [OptionsViewController toggleImage]: selector no reconocido enviado a la instancia 0x4b305c0 '. Tenga en cuenta que llamarlo sin el parámetro funcionaba bien (¡aunque obviamente no podía saber qué interruptor lo estaba llamando!) – Julian
Vaya, lo hice funcionar. ¡Asegúrese de agregar la acción objetivo para incluir los dos puntos en su llamada al selector! Esto funciona @selector (keychainOptionSwitched :), esto no @selector (keychainOptionSwitched). – Julian