如何通过选择器设置 textField becomeFirstResponder

How to set textField becomeFirstResponder via selector

我写了一个 UITextfield 扩展来构建我自己的自定义输入工具栏:

func addToolbar(selector: Selector? = nil) {
    let action = selector == nil ? #selector(resignFirstResponder) : selector
    
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: action)

    let toolbar = UIToolbar()
    toolbar.items = [flexibleSpace, doneButton]
    toolbar.sizeToFit()
    toolbar.layoutIfNeeded()
    
    inputAccessoryView = toolbar
    returnKeyType = .done
}

我是这样用的:

let myPickerView = UIPickerView()
myPickerView.dataSource = myPickerViewBehavior
myPickerView.delegate = myPickerViewBehavior
    
if let selected = value {
    let selectedIndex = PickableItemType.index(of: selected.rawValue)
    myPickerView.selectRow(selectedIndex, inComponent: 0, animated: true)
}
    
myTextField.delegate = self
myTextField.inputView = myPickerView
myTextField.addToolbar(selector: #selector(nextTextField.becomeFirstResponder))

正如您想象的那样,我希望在用户按下工具栏中的完成后出现下一个 UITextFieldbecomeFirstResponder。 但什么也没发生。 为了进行测试,我尝试在内部使用一个简单的 @objs funcprint 语句 and/or nextTextField.becomeFirstResponder,结果相同。 我也试过使用 UITapGestureRecognizer#selector(nextTextField.becomeFirstResponder),也是同样的结果。

所以我猜这与代码无法运行的#selector 机制有关。

P.S:如果 selector == niladdToolbar 内,#selector(resignFirstResponder) 工作正常。

我认为问题是由于 target: 设置 doneButton 错误造成的。

如果您还在 addToolbar: 方法中提供目标,例如:

func addToolbar(target: Any? = nil, selector: Selector? = nil) {
    let object = target ?? self
    let action = selector ?? #selector(resignFirstResponder)
    
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: object, action: action)

    let toolbar = UIToolbar()
    toolbar.items = [flexibleSpace, doneButton]
    toolbar.sizeToFit()
    toolbar.layoutIfNeeded()
    
    inputAccessoryView = toolbar
    returnKeyType = .done
}

这应该有效:

firstTextField.addToolbar(target: secondTextField, selector: #selector(becomeFirstResponder))

这是一个小测试结果:

https://imgur.com/R7R1ee9