你可以继承 UIApplication:
- 创建一个 UIApplication 子类
- 重写
-(BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
方法,记得调用超级实现
- 在实现中放置一个
NSLog
或其他诊断代码
例如,这将在每次按下 UIButton 时打印一个日志:
-(BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
{
if ([sender isKindOfClass:[UIButton class]])
{
NSLog(@"Action: %@ - %@ - %@", NSStringFromSelector(action), target, sender);
}
return [super sendAction:action to:target from:sender forEvent:event];
}
2013-07-08 14:46:18.270 UIApplicationSubclass[94764:c07] Action: anAction: - <ViewController: 0x76790a0> - <UIRoundedRectButton: 0x767b9b0; frame = (103 66; 73 44); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x767bad0>>
2013-07-08 14:46:27.378 UIApplicationSubclass[94764:c07] Action: anAction: - <ViewController: 0x76790a0> - <UIRoundedRectButton: 0x767b9b0; frame = (103 66; 73 44); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x767bad0>>
此外,要子类化 UIApplication,您必须像这样更改 main.m 文件(在我的情况下,UIApplication 子类名为 FLApplication,请查看 UIApplicationMain 函数的第三个参数和 FLApplication.h 的导入)
#import "AppDelegate.h"
#import "FLApplication.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([FLApplication class]), NSStringFromClass([AppDelegate class]));
}
}