Ios UIButton添加到inputAccessoryView未检测到触摸

Ios UIButton添加到inputAccessoryView未检测到触摸,ios,objective-c,xcode,uibutton,inputaccessoryview,Ios,Objective C,Xcode,Uibutton,Inputaccessoryview,我有一个inputAccessoryView,其中包括一个UIButton,但是UIButton对点击没有响应(甚至没有更改其图像)。我不知道为什么。以下是我相当简单的代码: UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)]; UIButton *selectionModeOkButton = [[UIButton alloc] initWithFrame:CGR

我有一个
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;
从:

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

此属性从UIView父类继承。此类将此属性的默认值更改为“否”

这告诉您,默认情况下,new
UIImageView
s已将
userInteractionEnabled
设置为
NO
。因此,添加这一行:

accessoryView.userInteractionEnabled = YES;

有关
userInteractionEnabled
工作原理的详细信息,请查看中该属性的条目(该属性从中继承的类)