在键盘扩展中设置按钮目标时遇到问题

时间:2017-01-12 21:57:08

标签: ios swift uitextfield uitextview swift-extensions

我已经看了很多扩展的例子,以为找不到这个。我只是尝试使用扩展来在swift中添加带有键盘顶部按钮的工具栏,但由于扩展不能轻易地存储值,是否有一种简单的方法可以将变量传递给扩展中每个按钮目标的选择器方法?例如,我想在点击保存时传递数据以保存到done()中的数据库。

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: [:])

0 个答案:

没有答案