views:

178

answers:

3

Hello,

I am developing an iPhone application with multiviews (Nav controller), but i like to receive an event if user touches in any view of the view. I understand it can be done by subclassing application delegate? If that's true how can i do it? My requirement is, i like to receive an event as soon as user touches any where in any view within my application.

Thanks for your help and time.

A: 

A way to do it is to use a Singleton class (which acts as an observer/mediator), which the application is an example of, in which you have viewControllers subscribe to when they are intersted in the touch events of a certain view. When the touch occurs the Singleton class is informed of the event as a result it informs all subscribers to the event of the event. Here is an example

   @interface MyEventClass
  {

  -(void)TouchEventDidOccur; 
  -(void)subscribeToTouchEvent:(id)delegate selector(selector):sel
  }

Above is the singleton class

now this is an example of what the view touchesBegan method might look like

-(void)touchesBegan...
{
    [[MyEventClass sharedInstance] TouchEventDidOccur];
}

and how one would subscribe to the event

 [[MyEventClass sharedInstance] subscribeToTouchEvent:self selector:@selector(receiveTouchEvent:)]

hope this helps

Daniel
Thanks Daniel. I see another thread for similar answerhttp://stackoverflow.com/questions/273450/iphone-detecting-user-inactivity-idle-time-since-last-screen-touchand would like to implement similar solution. With the approach you provided, i will have to implement this in each of the view. I've around 25 views in my application.
iamiPhone
http://stackoverflow.com/questions/273450/iphone-detecting-user-inactivity-idle-time-since-last-screen-touchI would like to know how can i subclass my app delegate from UIWindow
iamiPhone
unfortunatly you are not going to be able to get away with not modifying each of your 25 views to subscribe to the event, this is because you need to tell who ever is sending the event that you are interested in the event. application delegate is a protocol and you dont subclass it.
Daniel
The referenced posting is referring to subclassing UIApplication, not app delegate (or UIWindow). They're just having App Delegate itself be the UIApplication subclass, which I think is a little weird, but probably ok rather than creating a separate class to overload one method.
Rob Napier
+1  A: 

Your reference to subclassing UIApplication will work. Read down through the comments and it covers a somewhat quirky IMO way to implement it (by having the AppDelegate be a subclass of UIApplication). Myself, I would create a separate class to be the UIApplication subclass, rather than having the app delegate do both jobs, but I see the merit of either way.

That said, this is a very large and unusual stick and may suggest a design failure. What problem are you solving with this?

Rob Napier
A: 

What's wrong with using notifications? If you have disconnected classes across your application, it's trivial to have them listen for a particular notification, then have your views or view controllers post that notification when a touch event happens. All of the observers will take action on the notification.

Brad Larson