我通过创建xib视图以及自定义协议,视图控制器类和文本字段扩展来实现了自定义键盘。
我的问题是我不确定如何获取对当前文本字段的引用,以便在调用函数(List<int> years = yearsString.Split(',').Select(x => int.Parse(x.Trim())).ToList();
)时可以将其作为函数的属性,并在button函数中使用它。 (即像didEndEditing一样,由于自定义协议/委托而无法使用)
谢谢!
protocoll:
self.delegate?.enterPressed(textfield: currentTextfield)
视图控制器类:
@objc protocol ActivityKeyboardDelegate {
func numericKeyPressed(key: Int)
func numericBackspacePressed()
func numericSymbolPressed(symbol: String)
func enterPressed()
func dismissPressed()
}
}
文本扩展名:
class ActivityKeyboard: UIView {
// data
weak var delegate: ActivityKeyboardDelegate?
// MARK: - Initialization and lifecycle.
override init(frame: CGRect) {
super.init(frame: frame)
initializeKeyboard()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initializeKeyboard()
}
func initializeKeyboard() {
// set view
let xibFileName = "activityKeyboard"
let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)![0] as! UIView
self.addSubview(view)
view.frame = self.bounds
}
// MARK: - Button actions
@IBAction func numericButtonPressed(_ sender: UIButton) {
self.delegate?.numericKeyPressed(key: sender.tag)
}
@IBAction func backspacePressed(_ sender: AnyObject) {
self.delegate?.numericBackspacePressed()
}
@IBAction func symbolWasPressed(_ sender: UIButton) {
if let symbol = sender.titleLabel?.text, symbol.count > 0 {
self.delegate?.numericSymbolPressed(symbol: symbol)
}
}
@IBAction func enterPressed(_ sender: Any) {
print(self.superview!)
self.delegate?.enterPressed()
}
@IBAction func dismissPressed(_ sender: Any) {
self.delegate?.dismissPressed()
}
}