UIControl (addTarget:action:forControlEvents:) @selector 参数

发布于 2024-09-19 01:29:38 字数 582 浏览 1 评论 0原文

@selector(updateStuff:)

这会自动发送参数:(id)sender。是否可以将其覆盖为其他内容?在我的特定用法中,实际的发送者是没有意义的。

我正在使用 UITableView 并在每个单元格的 UIAccessoryView 插槽中设置 UISwitch。最初,我在单元初始化之外将 UISwitch 添加到 UIAccessoryView,这允许每个 UISwitch 成为唯一的实例。然而,在 iPhone 3G 上向下滚动长列表时,这会产生明显的延迟。如果我在初始化时将 UIAccessoryView 添加到单元格中,它可以出队而不是实例化,并大大缓解延迟。不幸的是,这样做的代价是,该实例对于表中的每个 UISwitch 来说并不是唯一的,并且 :(id)sender 变得毫无意义。

我需要两件事之一......

  1. 一种覆盖参数的方法 与@selector关联,所以我可以 替换 :(id)sender
  2. 或者比我使用的策略更好的策略 跟踪 UISwitches。

我总是可以在单元格创建/出列后添加 UISwitch,但它非常慢且效率低下。感谢您的任何帮助。

真挚地, Z@K!

@selector(updateStuff:)

This is automatically sends the parameter :(id)sender. Is it possible to override this to be something else? In my particular usage, the actual sender is meaningless.

I'm using a UITableView and setting a UISwitch in the UIAccessoryView slot on every cell. Originally, I was adding the UISwitch to the UIAccessoryView outside of the the cell initialization, which allowed each UISwitch to be a unique instance. However this creates a noticeable lag while scrolling down long list on the iPhone 3G. If I add the UIAccessoryView to the cell when it's initialized, it can be dequeued instead of instantiated and eases the lag considerably. Unfortunately, the cost of doing this is, the instance is NOT unique to each UISwitch in the table, and :(id)sender becomes meaningless.

I need one of two things...

  1. A way to override the parameter
    associated to @selector, so I may
    replace :(id)sender
  2. Or a better strategy than the one I am using to
    keep track of the UISwitches.

I can always resort to adding the UISwitch after the cell is created/dequeued but it is terribly slow and inefficient. Thanks for any help.

Sincerely,
Z@K!

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

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

发布评论

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

评论(3

情深如许 2024-09-26 01:29:46

我编写了一个 UISwitch 子类,其中包含用于值更改控制事件的基于块的处理程序,这在尝试跟踪哪个开关的值已更改时可以提供帮助。理想情况下,我们可以对组合而不是子类化做类似的事情,但这很适合我的需求。

https://gist.github.com/3958325

你可以这样使用它:

ZUISwitch *mySwitch = [ZUISwitch alloc] init];

[mySwitch onValueChange:^(UISwitch *uiSwitch) {
        if (uiSwitch.on) {
            // do something
        } else {
            // do something else
        }
    }];

你也可以使用它从 XIB 文件中,将开关拖到视图上,然后将其类更改为 ZUISwitch

I have written a UISwitch subclass with a block based hander for value change control events which can help when trying to track which switch's value has changed. Ideally, we could do something similar with composition rather than subclassing, but this works well for my needs.

https://gist.github.com/3958325

You can use it like this:

ZUISwitch *mySwitch = [ZUISwitch alloc] init];

[mySwitch onValueChange:^(UISwitch *uiSwitch) {
        if (uiSwitch.on) {
            // do something
        } else {
            // do something else
        }
    }];

You can also use it from a XIB file, by dragging a switch onto your view, and then changing its class to ZUISwitch

毁梦 2024-09-26 01:29:44

很多很多方法:

  • 设置标签。
  • 设置一个“关联对象”(参见 objc_setAssociatedObject())
  • UISwitch 子类。添加一些属性。
  • UITableViewCell 的子类。添加一些属性。向上导航视图层次结构,直到到达表视图单元格(类似于 while (v && ![v isKindOfClass:[UITableViewCell class]]) {v = v.superview; })。
  • 向上导航视图层次结构,如上所述。调用[tableView indexPathForCell:(UITableViewCell*)v]
  • 如果有多个表视图,您可以继续向上导航,直到找到 UITableView...

Many, many ways:

  • Set the tag.
  • Set an "associated object" (see objc_setAssociatedObject())
  • Subclass UISwitch. Add some properties.
  • Subclass UITableViewCell. Add some properties. Navigate up the view hierarchy until you reach a table view cell (something like while (v && ![v isKindOfClass:[UITableViewCell class]]) {v = v.superview; }).
  • Navigate up the view hierarchy, as above. Call [tableView indexPathForCell:(UITableViewCell*)v].
  • If there are multiple table views, you can continue navigating up until you hit a UITableView...
嗫嚅 2024-09-26 01:29:43

我有一个类似的问题,我解决它的方法是通过设置发件人的“标签”属性,每个视图都有这个属性,目的是帮助识别它。因此,在创建/出列单元格时,设置标记来标识单元格,并在操作中使用 [sender tag] 来获取单击的上下文

I had a similar problem, the way I've solved it is by setting the "tag" property of the sender, every view has this property and purpose is to help identify it. So while creating/dequeueing the cell set the tag to identify the cell, and on the action use [sender tag] to get the context of the click

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