【问题标题】:Is there a way to "capture" all the UIButton taps inside an app?有没有办法“捕获”应用程序内的所有 UIButton 点击​​?
【发布时间】:2014-12-17 15:35:50
【问题描述】:

我希望能够跟踪我的用户正在点击的按钮。 有没有办法“捕获”或“记录”我的应用程序中的所有按钮点击?

我在考虑方法调配,但我真的不想这样做。

【问题讨论】:

    标签: ios objective-c swift uibutton swizzling


    【解决方案1】:

    方面编程可以帮助你。看看这个库:

    https://github.com/steipete/Aspects

    基本上你会做这样的事情:

    [UIButton aspect_hookSelector:@selector(whateverSelectorYouWantToHookOn:) 
                      withOptions:AspectPositionAfter 
                       usingBlock:^(id<AspectInfo> aspectInfo) {
       NSLog(@"UIButton called");
    } 
    error:NULL];
    

    查看 AspectInfo 以获取有关所调用实例的更多信息。

    【讨论】:

      【解决方案2】:

      按钮可以附加多个操作。您始终可以为每个按钮添加日志记录操作。 action 方法接收发送者,因此您可以记录有关被点击按钮的信息。

      【讨论】:

      • 我想做一些更“通用”的事情,这将影响我应用中的所有按钮,而无需为每个按钮添加代码。
      【解决方案3】:

      您可以继承 UIButton 以添加日志记录。

      上接受的答案

      objective C: Buttons created from subclass of UIButton class not working

      很好地解释了为什么这是子类化可能有用的少数实例之一。

      基本上 UIButton 继承自 UIControl 和 UIControl Class Reference states

      子类化注意事项 您可能出于以下两个原因想要扩展 UIControl 子类:

      观察或修改针对特定事件将操作消息发送到目标的情况
      提供自定义跟踪行为(例如,更改突出显示的外观)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 2015-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多