UIButton添加到inputAccessoryView没有检测到触摸

时间:2014-10-20 14:16:20

标签: ios objective-c xcode uibutton inputaccessoryview

我有一个inputAccessoryView,其中包含UIButton,但UIButton没有响应点击(甚至没有更改它的图像)。我不知道为什么。这是我相当简单的代码:

UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];

UIButton *selectionModeOkButton = [[UIButton alloc] initWithFrame:CGRectMake(264, 11, 50, 46)];
[selectionModeOkButton setImage:[UIImage imageNamed:@"btn_count_ok_unpressed"] forState:UIControlStateNormal];
[selectionModeOkButton setImage:[UIImage imageNamed:@"btn_count_ok_pressed"] forState:UIControlStateHighlighted];
[selectionModeOkButton addTarget:self action:@selector(selectionModeOkButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[accessoryView addSubview:selectionModeOkButton];

self.selectionModeTextField.inputAccessoryView = accessoryView;

1 个答案:

答案 0 :(得分:2)

来自Apple docs for UIImageView

  

一个布尔值,用于确定是否忽略用户事件并从事件队列中删除。

     

此属性继承自UIView父类。此类将此属性的默认值更改为NO。

这告诉您,默认情况下,新的UIImageViewuserInteractionEnabled设置为NO。所以,添加这一行:

accessoryView.userInteractionEnabled = YES;

有关userInteractionEnabled的确切工作原理的详细信息,请查看Apple docs for UIView(继承该属性的类)中该属性的条目。