Shadowman
Shadowman

Reputation: 12039

Capture touch on UINavigationBar?

I am trying to capture a touch event on my UINavigationBar. When a user taps it, I'm planning to perform an action. However, I do not seem to be able to capture touch events on this component. My code looks like this...

   UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self.navigationController.navigationBar action:nil];
    singleTap.numberOfTapsRequired = 1;
    singleTap.delegate = self;
    [self.navigationController.navigationBar addGestureRecognizer:singleTap];

Whenever I tap the navigation bar, though, my selector is not called. Am I doing something wrong here? Do I need to do something special to capture touch events on this control?

Upvotes: 0

Views: 203

Answers (1)

Gabriel.Massana
Gabriel.Massana

Reputation: 8225

Try:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething)];
singleTap.numberOfTapsRequired = 1;
//singleTap.delegate = self;
[self.navigationController.navigationBar addGestureRecognizer:singleTap];



-(void) doSomething
{
    NSLog(@"doSomething");
}
  • Target should be self (the recipient of the action)
  • Action should be the selector that identifies the method implemented

Upvotes: 1

Related Questions