本文介绍了UIBarButtonItem:target-action不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 UIBarButtonItem
中有一个自定义视图,通过调用 -initWithCustomView
设置。
我的条形按钮项目呈现得很好,但是当我点击它时,它不会调用我的目标对象上的操作。
I've got a custom view inside of a UIBarButtonItem
, set by calling -initWithCustomView
.My bar button item renders fine, but when I tap it, it doesn't invoke the action on my target object.
这里是我的代码:
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适用于自定义视图。尝试使用UIButton而不是UIImageView,并将目标和操作应用到按钮。
I do not think the target and action of the UIBarButtonItem apply to custom views. Try using a UIButton instead of UIImageView and applying the target and action to the button.
Swift中的示例代码:
Sample code in 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
这篇关于UIBarButtonItem:target-action不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!