views:

391

answers:

1

Hi,

I am a little newbie regarding notifications on the iPhone.

What I am trying to do is this: I have an object using a class based on UIImageView. This class has its own implementations of touchbegan, touchesended, etc.

Is that possible for the main program to know when the user lifted his finger from the object? In other words, is it possible to create a notification from the class to the main application's view controller (using the class) so it will fire a method? The method should be on the main application, not on the class.

Is this possible? Can you give an example?

thanks for any help.

+1  A: 

In -touchesEnded:, add something like the following statement (changing handleTouchesEndedNotification to the unique name you want to use for the notification):

[[NSNotificationCenter defaultCenter] postNotificationName:@"handleTouchesEndedNotification" object:nil userInfo:nil];

In your main, application delegate or other class, add the following statement where the class is initialized, which listens for notifications with the unique name you used above:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMyNotification:) name:@"handleTouchesEndedNotification" object:nil];

Further into your class, add the method and logic:

- (void) handleMyNotification:(NSNotification *)notification {
    // do stuff...
}

If you want to pass data along with the notification, create an NSDictionary object and set the notification's userInfo variable to this dictionary object.

Alex Reynolds
THANKS!!!!!! YOU'RE D MAN!
Digital Robot