AddTarget不适用于UITextField rightView

时间:2014-03-28 11:18:56

标签: ios ios6 uibutton uitextfield

我正在添加以下代码,以便在UITextField右侧添加下拉按钮

self.filterTextView.rightViewMode = UITextFieldViewModeAlways;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0, 0, 32, 32)];
    [btn setBackgroundImage:[UIImage imageNamed:@"downArrow.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(showPicker:) forControlEvents:UIControlEventTouchUpInside];
    self.filterTextView.rightView = btn;

按钮在右侧sie上可见,但当我点按按钮时,showPicker方法无法调用

我在iOS 6中运行代码

1 个答案:

答案 0 :(得分:0)

我正在运行您的代码并且它正在为我工​​作。

此外,您可以在没有按钮的情况下进行管理,您可以使用UITextFieldDelegate

self.filterTextView.delegate = self

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.filterTextView)
    {
        [self.filterTextView resignFirstResponder];
        //here open picker
    }
}