jjzjj

objective-c - addTarget :action:forControlEvents - UISwitch in TableView - sender ok, 事件始终为 0x0

coder 2024-01-10 原文

利用这个论坛中的精彩帖子,我在 tableView 中创建了一个开关作为 accessoryView。当触摸开关时,我的 Action (switchChanged) 被调用。只有发送方有有效值,事件为0x0。

将目标添加到 switchView:

        [switchView addTarget:self action:@selector(switchChanged:forEvent:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];

行动:

- (void) switchChanged:(id)sender forEvent:(UIEvent *)event {
  if(event) {
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:self.tableView]];
    IFDFlightlogFormQuestions *question = [self.resultsController objectAtIndexPath:indexPath];
    NSLog(@"IFDNewFlightlogViewController_Pad:switchChanged Switch is %@ xmlAttrib %@",[sender isOn],question.XmlAttrib);
    [self.xmlResults setValue:([sender isOn])?@"true":@"false" forKey:question.XmlAttrib];
  }
}

使用 action:@selector(switchChanged: forEvent:) - 添加空格 - 没有变化。

使用 action:@selector(switchChanged::) - 删除了 forEvent - 无法识别的选择器。

我的目标是获取 tableView 的 indexPath,以便我可以更改字典中的值。

我目前的解决方法是只使用发件人信息,但我想要事件信息:

- (void) switchChanged:(id)sender forEvent:(UIEvent *)event {
    UISwitch *switchView = (UISwitch *)sender;
    UITableViewCell *cell = (UITableViewCell *)switchView.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    IFDFlightlogFormQuestions *question = [self.resultsController objectAtIndexPath:indexPath];
    NSLog(@"IFDNewFlightlogViewController_Pad:switchChanged Switch is %@ xmlAttrib %@",([sender isOn])?@"On":@"Off",question.XmlAttrib);
    [self.xmlResults setValue:([sender isOn])?@"true":@"false" forKey:question.XmlAttrib];        
}

关于获取事件信息的任何指示?

最佳答案

可以通过将目标添加到 UISwitch 来触发 @selector

[switchCtl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];

关于objective-c - addTarget :action:forControlEvents - UISwitch in TableView - sender ok, 事件始终为 0x0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5862956/

有关objective-c - addTarget :action:forControlEvents - UISwitch in TableView - sender ok, 事件始终为 0x0的更多相关文章

随机推荐