import UIKit
import FirebaseDatabase
extension UIViewController: UITextViewDelegate {
func addToolBar(textField: UITextView, mode: Int, ref: String , pkg: [String:Any]){
//mode: detailmsges = 2 ; home = 1
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor.gray
let doneButton = UIBarButtonItem(title: "Save", style: UIBarButtonItemStyle.done, target: self, action: #selector(donePressed))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
if mode == 1 {
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancelPressed))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
} else if mode == 2 {
toolBar.setItems([spaceButton, doneButton], animated: false)
}
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
textField.delegate = self
textField.inputAccessoryView = toolBar
}
//how can I receive the parameters that I get in addToolBar() here to save to DB?
func donePressed(){
view.endEditing(true)
}
func cancelPressed(){
view.endEditing(true)
}
}
然后我就像使用它一样:
addToolBar(textField: myTextField, mode: 1, ref: "", pkg: [:])