我需要打開鍵盤上的按鈕單擊UIButton(不使用/用於UITextField)。我試圖通過覆蓋變量canBecomeFirstResponder
來創建自定義按鈕,但它不起作用。iOS - UIButton成爲第一響應者打開鍵盤
有沒有其他方法可以做到這一點?
注意:我想將UIPIckerView設置爲鍵盤框架中UIButton的輸入視圖。
這是我的代碼。
class RespondingButton: UIButton {
override var canBecomeFirstResponder: Bool {
return true
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
// common init
}
}
在我的視圖控制器中,我連接了按鈕操作。
class TestViewController: UIViewController {
@IBAction func testBecomeFirstResponder(button: RespondingButton){
button.becomeFirstResponder() // Not working.
}
}
是否要使用鍵盤來更改UIPicker顯示的數據? – Phyber
@Phyber你可能知道,我們可以將UIPickerView設置爲UITextField的inputView,就像我用UIButton做的那樣 – Krunal
[UIBUTTON和picker view]的可能重複(https://stackoverflow.com/questions/6429900/uibutton-and-picker-view) – dan