troner
troner

Reputation: 239

Subclass of UIViewController don't catch touch event

I have a parent class (A) that is UIViewController. Than i create class B that is subclass of class A. What happens is that i can't catch touch events in class B with methods like touchesBegan. But if i implement this methods in class A ... they get called.

@interface A:UIViewController
.....

@interface B:A

Upvotes: 7

Views: 3346

Answers (3)

vgonisanz
vgonisanz

Reputation: 11940

To use a UIViewController, must do event like:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch;
    CGPoint pos;

    for( touch in touches )
    {
        //pos = [ touch locationInView:self ]; // Only work on UIView
        pos = [touch locationInView:self.view ];       // Work on UIViewController

        //NSLog(@"Touch: %f, %f",pos.x,pos.y);

        // Send X, Y, tapcount
        _faceOff->toucheBegan( pos.x, pos.y, [ [ touches anyObject ] tapCount ]);
    }
}

Hope it help.

Upvotes: 7

Mathieu
Mathieu

Reputation: 3130

You need to subclass UIView to implement the method touchesBegan.

@interface YourCustomView : UIView

@implementation YourCustomView

// Override this function to get the touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"TOUCH!");
}

And now set the view of your VC as a "YourCustomView"

@interface YourViewController : UIViewController
{
     YourCustomView*    view  
}

Upvotes: 4

Eiko
Eiko

Reputation: 25642

You need to implement those methods in your UIView subclasses, not in UIViewController subclasses.

Upvotes: 3

Related Questions