1
votes

I have a simple (view-based) application. I want on tapping on custom UIView my button moved somewhere inside that view (for example to point 10,10).

  1. My custom UIView is DrawView (DrawView.h and DrawView.m).
  2. RotatorViewController (h. and .m).

I add to my DrawView an UIButton, connect with outlets my DrawView and UIButton. I add UITapGestureRecognizer in RotatorViewController and @selector(tap:). Here is code of UITapGestureRecognizer

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:drawView action:@selector(tap:)];
    [drawView addGestureRecognizer:tapGR];
    [tapGR release];

}

@selector(tap:)

- (void) tap:(UITapGestureRecognizer *)gesture {
        myButton.transform = CGAffineTransformMakeTranslation(10, 10);
}

But when i tap anywhere in DrawView application crashes. Here is log from console

2011-02-23 20:59:24.897 Rotator[7345:207] -[DrawView tap:]: unrecognized selector sent to instance 0x4d0fa80
2011-02-23 20:59:24.900 Rotator[7345:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DrawView tap:]: unrecognized selector sent to instance 0x4d0fa80'

I need your help

2

2 Answers

2
votes

You said :

UITapGestureRecognizer in RotatorViewController and @selector(tap:)

and you wrote : UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:drawView action:@selector(tap:)];

It means the actions is performed in the drawView delegate but you defined the selector tap: in the RotatorViewController.

I think you just have to replace the target drawView by self

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

0
votes

The error indicates that instances of the DrawView class do not respond to tap: messages. I see that you have defined a tap: method in DrawView.m, but have you declared that method in the header? Like so:

DrawView.h

@class UITapGestureRecognizer;
@interface DrawView {
    UIButton *myButton;
}
- (void) tap:(UITapGestureRecognizer *)gesture;
@end