tags:

views:

2245

answers:

3

Apple is really funny. I mean, they say that this works:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];
    NSUInteger numTaps = [touch tapCount];
    if (numTaps < 2) {
        [self.nextResponder touchesBegan:touches withEvent:event];
   } else {
        [self handleDoubleTap:touch];
   }
}

I have a View Controller. Like you know, View Controllers inherit from UIResponder. That View Controller creates a MyView object that inherits from UIView, and adds it as a subview to it's own view.

So we have:

View Controller > has a View (automatically) > has a MyView (which is a UIView).

Now inside of MyView I put that code like above with a NSLog that prints "touched MyView". But I forward the event to the next responder, just like above. And inside the ViewController I have another touchesBegan method that just prints an NSLog a la "touched view controller".

Now guess what: When I touch the MyView, it prints "touched MyView". When I touch outside of MyView, which is the view of the VC then, I get a "touched view controller". So both work! But what doesn't work is forwarding the event. Because now, actually the next responder should be the view controller, since there's nothing else inbetween. But the event handling method of the VC is never called when I forward it.

%$&!§!!

Ideas, guys?

Figured out weird stuff MyView's next responder is the view of the view controller. That makes sense, because MyView is a subview of that. But I didn't modify this UIView from the view controller. it's nothing custom. And it doesn't implement any touch event handling. Shouldn't the message get passed on to the view controller? How could I just let it pass? If I remove the event handling code in MyView, then the event arrives nicely in the view controller.

+1  A: 

According to a similar question, your method should work.

This leads me to think that your view's nextResponder is not actually the ViewController, as you suspect.

I would add a quick NSLog in your forwarding code to check what your nextResponder really points to:

if (numTaps < 2) {
    NSLog(@"nextResponder = %@", self.nextResponder);
    [self.nextResponder touchesBegan:touches withEvent:event];
}

You can also change your other NSLog messages so that they output type and address information:

NSLog(@"touched %@", self);

touched <UIView 0x12345678>

This should get you started on diagnosing the problem.

e.James
Thanks eJames. The MyView tells me that next responder is exactly the view of the view controller. The memory adresses match perfect. however, this touchesBegan won't be called. but now I see that this method would be invoked on the view of the view controller, but not on the view controller itself. but then, wouldn't this event continue bubbling up to the view controller of the view doesn't handle it? or is that another special case because i send it manually?
HelloMoon
Well, I suppose that makes sense. The default implementation of `touchesBegan:` does nothing, so once you pass it on to the next `UIView` object, it simply ends. Is it feasible to simply pass the touch event on to the viewController manually?
e.James
Have you tried calling `[super touchesBegan:...]` instead of `[self.nextResponder touchesBegan:...]` I have seen some people using that method, but I'm not sure what it does.
e.James
that should have the same effect, because views are in a hierarchy. the super is not the controller, but the view of the controller.
HelloMoon
just noticed that I may be wrong with that. super != superview ;) a UIView's super should be UIResponder, so sending that to super makes no much sense I think. It would fall back on the view, maybe creating a loop?
HelloMoon
That's true, but I think `UIResponder` may forward the `touchesBegan` method to the appropriate superview automatically? I'm afraid I don't know all of the details of how the responder chain works.
e.James
I've been having the same problem. Here's what i read in Apple's docs: If you implement a custom view to handle events or action messages, you should not forward the event or message to nextResponder directly to send it up the responder chain. Instead invoke the superclass implementation of the current event-handling method—let UIKit handle the traversal of the responder chain.
Spanky
http://developer.apple.com/IPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html#//apple%5Fref/doc/uid/TP40007072-CH9-SW2
Spanky
+1  A: 

Yes it should 100% work in common case. I've just tried with test project and everything seems to be ok.

I've created View-Based Application. Using Interface Builder put new UIView up to the present View. Then create new file with subclass of UIView and select just created class for my new view. (Interface Builder->Class identity->Class->MyViewClass)

Add touches handler functions both for MyViewClass and UIViewController.

//MyViewClass

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"myview touches");
[self.nextResponder touchesBegan:touches withEvent:event];
}

//ViewController

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
  NSLog(@"controller touches");
}

I see both NSLogs when press MyViewClass. Did you use Interface Builder and XIB file when loading your ViewController or set view programatically with loadView function?

MikZ
A: 

I know this post is old but i thought id share because I had a similar experience. I fixed it by setting the userInteractionEnabled to NO for the view of the view controller that was automatically created.

twerdster