0

我在我的代码中打开手势识别器时遇到了麻烦。我向点击手势识别器添加了一个 handleTap 回调,但打印语句从未发生。我只是没有运气。有人看到我在这里做错了吗?

在我的 ViewController.h

@interface ViewController : UIViewController<UITextFieldDelegate, UIGestureRecognizerDelegate> {

}

@end

这就是我在 ViewController.m 中的内容

        - (void)viewDidLoad
    {
        [super viewDidLoad];


        CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
        View *myview = [[View alloc] initWithFrame:applicationFrame];



        myview.userInteractionEnabled = YES;
        myview.multipleTouchEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(handleTap)];
        tap.numberOfTapsRequired = 1;
        tap.delegate = self;
        [myview addGestureRecognizer:tap];

        self.view = myview;
    }

-(void)handleTap:(UITapGestureRecognizer*)recognizer {
    NSLog(@"dismiss keyboard");
    //[usernameTextField resignFirstResponder];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return YES;
}

编辑:我添加了一个文本字段,当我单击文本字段时,我看到了分接选择器打印语句。但是,如果我点击文本字段,我不会得到选择器打印语句。

4

3 回答 3

1

I think the problem is with your selector. Try changing

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(handleTap)];

And replace it with

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(handleTap:)];

To make the selector match the signature for handleTap.

于 2012-12-17T22:53:03.703 回答
1

You are passing the wrong selector when initialising.

The line should read like this:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(handleTap:)];

Notice the colon after handleTap to match the method you want to use.

于 2012-12-17T22:53:35.810 回答
0

Apparently subclassing the UIView and overriding drawrect with an empty function caused the selector to be called.

于 2012-12-18T03:49:25.110 回答