#selector 函数不会为 UIDatePicker 触发
#selector function does not trigger for UIDatePicker
我有一个非常简单的 CustomIDatePicker
class 写在 Swift 5,它的顶部有一个工具栏,其中包含一个 'Done' 按钮。问题是,与此按钮关联的功能似乎从未被触发。
class CustomDatePicker: UIDatePicker {
var zTextField: UITextField!
let toolbar = UIToolbar()
// bar buttons
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed(sender:)))
convenience init(textField: UITextField) {
self.init()
toolbar.sizeToFit()
toolbar.setItems([doneBtn], animated: true)
self.datePickerMode = .date
// link up the Text Field
zTextField = textField
zTextField.inputAccessoryView = toolbar
zTextField.inputView = self
}
@objc func donePressed (sender: UIBarButtonItem) {
print("done pressed") // this line fail to appear on the debugger
}
}
一个非常简单的 ViewController 来测试 class:
class ViewController: UIViewController {
@IBOutlet weak var theTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let datePicker = CustomDatePicker(textField: theTextField)
}
}
如有任何帮助,我们将不胜感激!
您需要设置目标,为此您需要在组件初始化中移动条形按钮实例化。
class CustomDatePicker: UIDatePicker {
var zTextField: UITextField!
let toolbar = UIToolbar()
// bar buttons
convenience init(textField: UITextField) {
self.init()
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(donePressed(sender:)))
toolbar.sizeToFit()
toolbar.setItems([doneBtn], animated: true)
self.datePickerMode = .date
// link up the Text Field
zTextField = textField
zTextField.inputAccessoryView = toolbar
zTextField.inputView = self
}
@objc func donePressed (sender: UIBarButtonItem) {
print("done pressed") // this line fail to appear on the debugger
}
}
我有一个非常简单的 CustomIDatePicker
class 写在 Swift 5,它的顶部有一个工具栏,其中包含一个 'Done' 按钮。问题是,与此按钮关联的功能似乎从未被触发。
class CustomDatePicker: UIDatePicker {
var zTextField: UITextField!
let toolbar = UIToolbar()
// bar buttons
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed(sender:)))
convenience init(textField: UITextField) {
self.init()
toolbar.sizeToFit()
toolbar.setItems([doneBtn], animated: true)
self.datePickerMode = .date
// link up the Text Field
zTextField = textField
zTextField.inputAccessoryView = toolbar
zTextField.inputView = self
}
@objc func donePressed (sender: UIBarButtonItem) {
print("done pressed") // this line fail to appear on the debugger
}
}
一个非常简单的 ViewController 来测试 class:
class ViewController: UIViewController {
@IBOutlet weak var theTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let datePicker = CustomDatePicker(textField: theTextField)
}
}
如有任何帮助,我们将不胜感激!
您需要设置目标,为此您需要在组件初始化中移动条形按钮实例化。
class CustomDatePicker: UIDatePicker {
var zTextField: UITextField!
let toolbar = UIToolbar()
// bar buttons
convenience init(textField: UITextField) {
self.init()
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(donePressed(sender:)))
toolbar.sizeToFit()
toolbar.setItems([doneBtn], animated: true)
self.datePickerMode = .date
// link up the Text Field
zTextField = textField
zTextField.inputAccessoryView = toolbar
zTextField.inputView = self
}
@objc func donePressed (sender: UIBarButtonItem) {
print("done pressed") // this line fail to appear on the debugger
}
}