iphone - UIBarButtonItem : target-action not worki

我在 UIBarButtonItem 中有一个自定义 View ,通过调用 -initWithCustomView 进行设置。 我的条形按钮项目呈现正常,但是当我点击它时,它不会调用我的目标对象上的操作。

这是我的代码:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage.png"]];
UIBarButtonItem *bbItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = bbItem;
[imageView release];
[bbItem setTarget:self];
[bbItem setAction:@selector(deselectAll)];

最佳答案

我不认为 UIBarButtonItem 的目标和操作适用于自定义 View 。尝试使用 UIButton 而不是 UIImageView 并将目标和操作应用于按钮。

Swift 中的示例代码:

let button  = UIButton(type: .Custom)
if let image = UIImage(named:"icon-menu.png") {
    button.setImage(image, forState: .Normal)
}
button.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)
button.addTarget(self, action: #selector(MyClass.myMethod), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = barButton

关于iphone - UIBarButtonItem : target-action not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2796438/

相关文章:

iphone - 如何覆盖@synthesized getter?

objective-c - @interface 和 @protocol 解释?

ios - 如何关闭 Storyboard 弹出框

iphone - 你需要在 GCD 的一个 block 中创建一个 NSAutoreleasePoo

objective-c - 整数总是初始化为0吗?

objective-c - iOS 8 iPhone 上的 UIPopoverPresentatio

ios - 通过 iOS 创建和导出动画 gif?

iphone - 实现 copyWithZone : 时的最佳实践

ios - 如何从 UIPickerView 获取选定的值

iphone - iOS 7 UIRefreshControl tintColor 不适用于 beg