i´ve made lots of my own 开发者_StackOverflow社区"CustomUIButton" in a for-loop in my viewcontroller. In this "CustomUIButton"-class i´ve implemented an UIGestureRecognizer like this:
(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// custom things.
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[self addGestureRecognizer:longPress];
[longPress release];
}
}
- (void) handleLongPress:(UILongPressGestureRecognizer*) recognizer{
if (recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"Long press Ended");
}
else {
NSLog(@"Long press detected.");
// Do something
}
}
If i init the target with "self", my "handleLongPress"-function in this class will be called. It´s cool. If i init the target with "nil", it should check the parent viewcontroller, right?
Any ideas why an additional function with the same name in my viewcontroller won´t be called? (For this test i´ve commented the "longpress"-function of the button-class out.)
In the docs for UIGestureRecognizer's initWithTarget:action:
method, for the target
parameter it says:
An object that is the recipient of action messages sent by the receiver when it recognizes a gesture. nil is not a valid value.
Note the last sentence.
The docs also say this which should explain why it doesn't work:
A gesture recognizer does not participate in the view’s responder chain.
You must specify a value for target
.
精彩评论