我想在自定义标题类中添加click事件。 以下是我的代码。当我点击Header区域时,它会给出EXC_BAD_ACCESS。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
}
return self;
}
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
//Do stuff here...
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
答案 0 :(得分:1)
//At View did load
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapRecognizer];
- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
//addcodehere
}
答案 1 :(得分:0)
如果您的自定义标头类是UIView
的子类,则表示使用self
代替self.view
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapRecognizer.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapRecognizer];
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
//Do stuff here...
}
答案 2 :(得分:0)
这可能会对你有所帮助......
{
UITapGestureRecognizer *tapGestureRecognizer;
self.view.backgroundColor = [UIColor whiteColor];
/* Create the Tap Gesture Recognizer */
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTaps:)];
self.tapGestureRecognizer.numberOfTouchesRequired = 2;
self.tapGestureRecognizer.numberOfTapsRequired = 3;
[self.view addGestureRecognizer:self.tapGestureRecognizer];
}
- (void) handleTaps:(UITapGestureRecognizer*)paramSender
{
NSUInteger touchCounter = 0; for (touchCounter = 0;
touchCounter < paramSender.numberOfTouchesRequired;
touchCounter++)
{
CGPoint touchPoint =
[paramSender locationOfTouch:touchCounter inView:paramSender.view];
NSLog(@"Touch #%lu: %@",(unsigned long)touchCounter+1, NSStringFromCGPoint(touchPoint));
}
}
答案 3 :(得分:0)
而不是在init方法中尝试在ViewDidLoad中执行。
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
// Do any additional setup after loading the view from its nib.
}
并在类扩展中包含此方法,
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer ;
或者在viewDidLoad之前写一下。