我有一个UISwitch
内部自定义UITableViewCell
(我称之为的子类RootLabeledSwitchTableCell
)。
单元格包含 aUILabel
和UISwitch
彼此相邻。
我有一个指向此自定义单元格内的开关的@property
调用:keychainSwitch
@interface RootLabeledSwitchTableCell : UITableViewCell {
IBOutlet UILabel *textLabel;
IBOutlet UISwitch *labeledSwitch;
}
@property (nonatomic, retain) IBOutlet UILabel *textLabel;
@property (nonatomic, retain) IBOutlet UISwitch *labeledSwitch;
@end
在我的表视图委托中,我添加了一个选择器,如果切换状态被翻转,则调用该选择器:
- (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;
}
// ...
}
}
// ...
}
}
所以这个选择器可以正常工作:
- (void) keychainOptionSwitched {
NSLog(@"Switched keychain option from %d to %d", ![self.keychainSwitch isOn], [self.keychainSwitch isOn]);
}
但是,我不能使用UISwitch
实例的-setOn:animated:
方法来初始化其初始状态:
- (void) updateInterfaceState {
BOOL isFirstTimeRun = [[[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstTimeRunKey] boolValue];
if (isFirstTimeRun) {
[self.keychainSwitch setOn:NO animated:NO];
}
}
从测试来看,self.keychainSwitch
这nil
导致-setOn:animated
什么都不做,但我仍然可以操作开关,并且NSLog
语句将正确打印开关状态更改,例如:
[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
self.keychainSwitch
在UITableView
委托方法中设置我有什么遗漏吗?