addTarget:action:forControlEvents - TableView 中的 UISwitch - 发送者正常,事件始终为 0x0

发布于 2024-11-04 11:01:25 字数 1840 浏览 8 评论 0原文

使用这个论坛中精彩的帖子,我创建了一个开关作为 tableView 中的accessoryView。当触摸开关时,我的操作(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 的索引路径,以便我可以更改字典中的值。

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

- (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];        
}

有关获取有关此事件信息的任何指示吗?

Using the fantastic posts in this forum, I created a switch as a accessoryView in a tableView. When the switch is touched my action (switchChanged) is called. Only the sender has a valid value, the event is 0x0.

Adding target to the switchView:

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

The Action:

- (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];
  }
}

Using action:@selector(switchChanged: forEvent:) - Added space - no change.

Using action:@selector(switchChanged::) - removed forEvent - unrecognized selector.

My goal is to get the indexPath to the tableView so I can change the value in my dictionary.

My current workaround is to use just the sender information, but I would like the event information:

- (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];        
}

Any pointers on getting the event information on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尬尬 2024-11-11 11:01:25

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

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

It is possible to trigger @selector by adding a target to the UISwitch.

[switchCtl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
迟月 2024-11-11 11:01:25

另一种可能性是在 UISwitch 上设置标签属性以匹配表视图的行。如果您也使用多个部分,那么您可以想出一些解决方案将它们编码为单个整数值。请记住,使用这种方法,每次更新单元格 (tableView:cellForRowAtIndexPath:) 时都需要更新标记,因为当重复使用单元格时,行可能会发生变化。

Another possibility is to set the tag property on the UISwitch to match the row of your tableview. If you use multiple sections as well, then you could come up with some solution to encode these in a single integer value. Keep in mind that with this approach, you need to update the tag every time you update the cell (tableView:cellForRowAtIndexPath:) since the row can change as cells are reused.

贩梦商人 2024-11-11 11:01:25

不幸的是,UIControl 操作没有发送 forEvent:...只有第一部分,这就是为什么您看不到任何值的原因。

您的解决方法的工作方式是一种合适的方法。

另一种方法是对 UISwitch 进行子类化,以便它保留对 NSIndexPath 的引用,甚至重写

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

以中断并发送自定义事件...

Unfortunately no forEvent: is sent from a UIControl action... only the first part which is why you are not seeing any value.

The way your workaround works is a suitable method.

Another way would be to subclass the UISwitch so it held a reference to the NSIndexPath, or even overrides

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

to interrupt and send a custom event...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文