Hi everyone,
I'm trying to capture events on a MKMapView and I came across a thread which suggested that I subclass UIWindow, override the function sendEvent and post a notification within this function. Here it is
Intercepting/Hijacking iPhone Touch Events for MKMapView - Stack Overflow
I have implemented it in my code as follows (this is my subclassed UIWindow)
CustomWindow.m
Code:
#import "CustomWindow.h"
@implementation CustomWindow
-(void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
// let's send a notification off
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:event forKey:@"event"];
NSNotification *note = [NSNotification notificationWithName:@"touchEvent" object:self userInfo:dictionary];
[[NSNotificationCenter defaultCenter] postNotification:note];
NSLog(@"notification sent");
}
@end
And I try to listen for this notification in a view controller as follows:
MapViewController.m
Code:
@implementation MapViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
//let's listen for the touch notifications
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(receivedTouchEvent:) name:@"touchEvent" object:nil];
}
return self;
}
-(void)receivedTouchEvent:(NSNotification *)note
{
NSLog(@"mapView controller has received event");
NSDictionary *userInfo = [note userInfo];
NSLog(@"dictionary %@", userInfo);
}
- (void)dealloc {
[super dealloc];
}
@end
The problem is that I don't receive the notifications in my view controller. Am I missing some fundamental point of NSNotification?