UIBarbuttonItem不调用action方法

时间:2014-07-21 16:10:20

标签: objective-c ios7 xcode5 uinavigationbar uinavigationitem

我正在使用此代码添加导航栏按钮。

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);

按钮显示,但它不会调用其动作方法。

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

请帮忙!

1 个答案:

答案 0 :(得分:1)

您还应该设置目标:

self.navigationItem.rightBarButtonItem.target = self;

您还可以使用自定义视图创建条形按钮项目:

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];