#import
<objc/objc.h>
#import <objc/runtime.h>
- (void)sendEventHooked:(UIEvent *)event {
//在這裡做你想做的事情吧
NSLog(@"截獲事件:%@",
[eventdescription]);
//執行原來的訊息傳遞流程
[selfperformSelector:@selector(sendEventOriginal:)withObject:event];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
Method sendEvent =class_getInstanceMethod([UIWindowclass],@selector(sendEvent:));
Method sendEventMySelf =class_getInstanceMethod([selfclass],
@selector(sendEventHooked:));
IMP sendEventImp =method_getImplementation(sendEvent);
class_addMethod([UIWindowclass],@selector(sendEventOriginal:),
sendEventImp,method_getTypeEncoding(sendEvent));
IMP sendEventMySelfImp =method_getImplementation(sendEventMySelf);
class_replaceMethod([UIWindowclass],@selector(sendEvent:),
sendEventMySelfImp,method_getTypeEncoding(sendEvent));
self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
// Override point for customization after application launch.
if ([[UIDevicecurrentDevice]userInterfaceIdiom]
==UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewControlleralloc]initWithNibName:@"ViewController_iPhone"bundle:nil]autorelease];
}else {
self.viewController = [[[ViewControlleralloc]initWithNibName:@"ViewController_iPad"bundle:nil]autorelease];
}
self.window.rootViewController =self.viewController;
[self.windowmakeKeyAndVisible];
return YES;
}
然後可以在其它地方建立按鈕,就可以截獲到按鈕觸摸事件!