有没有办法将一些代码挂到-sendAction:to:from:forEvent:在UIApplication中?

时间:2009-08-07 14:00:18

标签: iphone events

我怎么能这样做?我想抓住这些事件,看看我的整个应用程序中发生了什么。我必须将UIApplication子类化吗?

UIControl在事件发生时调用此方法。看起来如果我将UIControl子类化,那么我就无法更深入地了解事件的细节。我可以指定那些事件mitmasks并使用(id)sender参数调用一些选择器,但是,我不会看到例如触摸坐标或类似的东西。

1 个答案:

答案 0 :(得分:2)

您可以使用方法调配,并提供自己的实现。

@implementaion UIApplication (MyStuff)
- (BOOL)_my_sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
{
    // do your stuff here
    // ...

    // call original selector
    [self _orig_sendAction:action to:target from:sender forEvent:event];
}
@end


BOOL DTRenameSelector(Class _class, SEL _oldSelector, SEL _newSelector)
{
    Method method = nil;

    // First, look for the methods
    method = class_getInstanceMethod(_class, _oldSelector);
    if (method == nil)
        return NO;

    method->method_name = _newSelector;
    return YES;
}

您只需要立即在应用程序启动时交换两个选择器:

Class UIApplicationCls = objc_getClass("UIApplication");
DTRenameSelector(UIApplicationCls, @selector(sendAction:to:from:forEvent:), @selector(_orig_sendAction:to:from:forEvent:);
DTRenameSelector(UIApplicationCls, @selector(_my_sendAction:to:from:forEvent:), @selector(sendAction:to:from:forEvent:);