-1

I've hidden the navigation bar so I can have a custom UIToolBar up there, but when I set the action property for the toolbar item to a method that pops it, it won't work, and I think it may be because I hid the navigation bar.

Here's my code:

[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Articles" style:UIBarButtonItemStyleBordered target:nil action:@selector(backButtonTapped)]];

...

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

But nothing happens.

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

2 Answers2

3

The target for your selector is nil when it should be self and you need to put the sender parameter in your action method!

[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Articles" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonTapped:)]];

-(void) backButtonTapped: (id) sender {
   //code as before here
}

edit As @sulthan noted, the sender parameter is not needed! You can leave it out as you did before!

Mario
  • 4,530
  • 1
  • 21
  • 32
  • That did not change anything. – Doug Smith Apr 27 '13 at 02:23
  • Please add a breakpoint to the popViewcontrollerAnimated: method and tell us if it is hit when you click the button – Mario Apr 27 '13 at 06:31
  • The `sender` parameter is optional. – Sulthan Apr 27 '13 at 18:24
  • @Sulthan can you give a reference o the docs? I didn't find that in the documentations an wasn't sure about that. Since I'm currently unable to compile th code myself, I thought that this was the problem. – Mario Apr 27 '13 at 18:27
  • @DougSmith be sure it has nothing to do with hiding the nav bar! Does the button you add ("Articles") appear in the toolbar? Is the backButtonTapped method inside your viewController on a method of another object? – Mario Apr 27 '13 at 18:40
  • @Mario [Target-Action in UIKit](http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW44) There are three possible selector signatures. – Sulthan Apr 27 '13 at 18:52
  • Thanks @Sulthan you're right! Can't see what the problem is then – Mario Apr 27 '13 at 18:56
  • @Mario The code is okey with the exception of the `nil` target. A different error cannot be found without more code. – Sulthan Apr 27 '13 at 19:09
  • I answered with what was the problem. Thanks so much for the help, though, I'm sorry I didn't include more code but I didn't realize I had to. – Doug Smith Apr 28 '13 at 15:21
0

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.

Community
  • 1
  • 1
Doug Smith
  • 29,668
  • 57
  • 204
  • 388