Search code examples
iosobjective-cuibarbuttonitemuitoolbar

UIBarButtonItem not firing its action method


In viewDidLoad: - (void)viewDidLoad { [super viewDidLoad];

[self.navigationController setNavigationBarHidden:YES animated:NO];

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Articles" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonTapped)]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Source" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Aa" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Rabbit" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];

Method:

- (void)backButtonTapped {
    [self.navigationController popViewControllerAnimated:YES];
}

I have a breakpoint on the call inside the method but it never gets called. Why is this method never getting called?


Solution

  • I had a UITapGestureRecognizer on the whole view that intercepted the tap on the UIBarButton. I solved it thanks to this answer, which basically stopped the UITapGestureRecognizer from beginning unless it was outside of the UIToolBar.