UIButton in a custom UIView trouble
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).
- My custom UIView is DrawView (DrawView.h and DrawView.m).
- 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
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:)];
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
精彩评论