Ho un UISwitch
all'interno di una UITableViewCell
personalizzata (la sottoclasse di cui si chiama RootLabeledSwitchTableCell
).UISwitch interno personalizzato UITableViewCell non disponibile
La cella contiene uno UILabel
e UISwitch
uno accanto all'altro.
Ho un @property
chiamato keychainSwitch
che punta allo switch all'interno di questa cella personalizzato:
@interface RootLabeledSwitchTableCell : UITableViewCell {
IBOutlet UILabel *textLabel;
IBOutlet UISwitch *labeledSwitch;
}
@property (nonatomic, retain) IBOutlet UILabel *textLabel;
@property (nonatomic, retain) IBOutlet UISwitch *labeledSwitch;
@end
A mio tavolo vista delegato, ho aggiunto un selettore che si chiama se lo stato interruttore è azionato:
- (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;
}
// ...
}
}
// ...
}
}
Quindi questo selettore funziona correttamente:
- (void) keychainOptionSwitched {
NSLog(@"Switched keychain option from %d to %d", ![self.keychainSwitch isOn], [self.keychainSwitch isOn]);
}
Tuttavia, non posso utilizzare il metodo -setOn:animated:
dell'istanza UISwitch
per inizializzare suo stato iniziale:
- (void) updateInterfaceState {
BOOL isFirstTimeRun = [[[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstTimeRunKey] boolValue];
if (isFirstTimeRun) {
[self.keychainSwitch setOn:NO animated:NO];
}
}
Da test, self.keychainSwitch
è nil
che sta causando -setOn:animated
di non fare nulla, ma posso ancora azionare l'interruttore e la dichiarazione NSLog
stamperà correttamente i cambiamenti di stato degli interruttori, ad esempio:
[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
c'è qualcosa che mi manca sull'impostazione self.keychainSwitch
nel metodo UITableView
delegato?
Questo non ha funzionato sulla mia fine. Grazie per il suggerimento, però. –
Non ha funzionato neanche qui. Ho ottenuto: [OptionsViewController toggleImage]: selettore non riconosciuto inviato all'istanza 0x4b305c0 '. Nota che chiamarlo senza il parametro ha funzionato bene (anche se ovviamente non sapevo quale interruttore lo stava chiamando!) – Julian
Oops, ha funzionato. Assicurati di aggiungere l'azione di destinazione per includere i due punti nella tua chiamata di selezione! Questo funziona @selector (keychainOptionSwitched :), questo non @selector (keychainOptionSwitched). – Julian