PO(NSStringFromSelector(self.editSettings.action));
[self.editSettings setCustomView:imageView]; //works if this is commented out but with bad image
PO(NSStringFromSelector(self.editSettings.action));
该代码产生
2013-01-17 11:47:41.184 BadgerNew[29570:c07] NSStringFromSelector(self.editSettings.action): Setting:
2013-01-17 11:47:41.185 BadgerNew[29570:c07] NSStringFromSelector(self.editSettings.action): Setting:
所以选择器仍然是相同的。
然而,在[self.editSettings setCustomView:imageView]之后;按钮无法再按下。按下它没有任何效果。
我想也许customView“吸收”用户交互。
但是,我无法设置imageView.userInteractionEnabled
。没有这样的选择器。
我错过了什么?
这似乎不是问题。我添加了更多控制台打印。它显示imageView userInteractionEnabled默认为false
UIImage * imageSetting= [UIImage imageNamed:@"setting-button"];
UIImageView * imageView = [[UIImageView alloc]initWithImage:imageSetting];
PO(NSStringFromSelector(self.editSettings.action));//Bar Button Item
self.editSettings.customView =imageView;
PO(NSStringFromSelector(self.editSettings.action));
PO(@(imageView.userInteractionEnabled));
PO(self.editSettings.customView);
PO(imageView);
imageView.userInteractionEnabled=false;
然后我尝试了完全没有Interface Builder(IB)解决方案并执行:
UIImage * imageSetting= [UIImage imageNamed:@"setting-button"];
UIImageView * imageView = [[UIImageView alloc]initWithImage:imageSetting];
self.editSettings = [[UIBarButtonItem alloc]initWithCustomView:imageView];
self.editSettings.action = @selector(Setting:);
self.navigationItem.leftBarButtonItem = self.editSettings;
仍然无效。
有人说customView需要是按钮而不是图像。好吧,我想知道。
答案 0 :(得分:3)
来自doc:
initWithCustomView:
initWithCustomView:
使用指定的自定义视图初始化新项目。 - (id)initWithCustomView:(UIView *)customView Parameters
customView
A custom view representing the item.
返回值
具有指定属性的新初始化项目。讨论
此方法创建的条形按钮项不会调用操作 响应用户交互的目标方法。相反, bar按钮项目需要指定的自定义视图来处理任何用户 互动并提供适当的回应。可用性