2010-09-29 74 views
7

我有一个父类(A),它是UIViewController。比我创建B类是类A的子类。发生什么事是我不能用类似touchesBegan的方法在类B中捕捉触摸事件。但是如果我在类A中实现这个方法......他们会被调用。UIViewController的子类不捕捉触摸事件

@interface A:UIViewController 
..... 

@interface B:A 

回答

3

你需要实现你的UIView子类的方法,而不是在UIViewController子类。

+0

嗯...我想我不明白。我在我的问题中添加了更多信息。 – troner 2010-09-29 15:17:07

+0

现在一切正常。这是我的错误。我有一些错误。我的子类工程:)谢谢你的回答! – troner 2010-09-29 16:30:54

4

您需要继承UIView以实现touchesBegan方法。

@interface YourCustomView : UIView 

@implementation YourCustomView 

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

现在设置你的VC的观点作为 “YourCustomView”

@interface YourViewController : UIViewController 
{ 
    YourCustomView* view 
} 
+0

是的,好的,但是这改变了架构。我真的无能为力吗?也许链接或启用的东西。 – troner 2010-09-29 15:57:47

+0

我最近遇到了同样的问题,而且在体系结构中只有很小的变化。 – MathieuF 2010-09-29 16:30:32

+1

现在一切正常。这是我的错误。我有一些错误。我的子类工程:)谢谢你的回答! – troner 2010-09-29 16:30:33

7

要使用一个UIViewController,必须做的事件,如:

- (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 ]); 
    } 
} 

希望它能帮助。