我向其中一个视图控制器中添加了一个注释框
private lazy var toolbarView: KeyboardToolbar = {
guard let keyboardToolbar = Bundle.main.loadNibNamed("KeyboardToolbar", owner: self, options: nil)?.first as? KeyboardToolbar else {
fatalError("Error")
}
return keyboardToolbar
}()
override var inputAccessoryView: KeyboardToolbar? {
return toolbarView
}
现在我在键盘工具栏的快捷文件中
@IBAction func sendPressed(_ sender: Any) {
//called on click of send button in the comment box
}
如何在viewcontroller代码中编写一个函数,该函数在注释框中的sendPressed上执行。
全键盘工具条码
class KeyboardToolbar: UIView, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
override func awakeFromNib() {
super.awakeFromNib()
textView.delegate = self
autoresizingMask = .flexibleHeight
textView.isScrollEnabled = false
textView.text = "Comment"
textView.textColor = UIColor.lightGray
textView.becomeFirstResponder()
}
override var intrinsicContentSize: CGSize {
let textSize = textView.sizeThatFits(CGSize(width: textView.bounds.size.width, height: CGFloat.greatestFiniteMagnitude))
return CGSize(width: bounds.width, height: textSize.height)
}
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.textColor == UIColor.lightGray {
textView.text = nil
textView.textColor = UIColor.black
}
}
func textViewDidChange(_ textView: UITextView) {
invalidateIntrinsicContentSize()
}
@IBAction func sendPressed(_ sender: Any) {
}
}
答案 0 :(得分:-1)
您可以为您KeyboardToolBar
创建一个自定义协议
protocol KeyboardToolBarDelegates: class {
func sendPressed(_ text: String)
}
现在您可以在KeyboardToolBar中创建委托对象
class KeyboardToolbar: UIView, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
weak var delegate: KeyboardToolBarDelegates?
@IBAction func sendPressed(_ sender: Any) {
delegate?.sendPressed("Pass your text")
}
}
视图控制器中的实现委托:
class mainViewController: UIViewController, KeyboardToolBarDelegates {
private lazy var toolbarView: KeyboardToolbar = {
guard let keyboardToolbar = Bundle.main.loadNibNamed("KeyboardToolbar", owner: self, options: nil)?.first as? KeyboardToolbar else {
fatalError("Error")
}
keyboardToolbar.delegate = self
return keyboardToolbar
}()
override var inputAccessoryView: KeyboardToolbar? {
return toolbarView
}
//Add protocol method
func sendPressed(_ text: String) {
//Method will be called when send pressed from the KeyboardToolBar
}
}