Ali Sufyan
Ali Sufyan

Reputation: 2046

UIBarbuttonItem not calling action method

I am using this code to add a navigationbar button.

UIColor *green = [UIColor colorWithRed:0.1 green:0.7 blue:0.2 alpha:1.0];
self.navigationItem.rightBarButtonItem = [KHFlatButton barButtonWithTitle:@"Go Pro!" backgroundColor:green];
self.navigationItem.rightBarButtonItem.action =@selector(goProButtonTapped);

Button shows up, but it doesn't call its action method.

- (void) goProButtonTapped
{
    NSLog(@"Go Pro button Tapped");
}

Please, help!

Upvotes: 0

Views: 707

Answers (1)

Sviatoslav Yakymiv
Sviatoslav Yakymiv

Reputation: 7935

You should also set target:

self.navigationItem.rightBarButtonItem.target = self;

You can also create bar button item with custom view:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIColor *green = [UIColor colorWithRed:0.1 green:0.7 blue:0.2 alpha:1.0];
button.backgroundColor = green;
[button addTarget:self action:@selector(goProButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Go Pro!" forState: UIControlStateNormal];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

Upvotes: 1

Related Questions