0

我想在我的自定义标题类中添加点击事件。以下是我的代码。当我单击标题区域时,它会给出 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.
}
4

4 回答 4

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
}
于 2013-02-13T11:39:50.267 回答
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...
}
于 2013-02-13T11:37:49.597 回答
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));
    }
}
于 2013-02-13T11:40:52.620 回答
0

尝试在 ViewDidLoad 中执行,而不是在 init 方法中执行。

- (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之前。

于 2013-02-13T11:41:14.647 回答