自定义键盘中的PickerView

时间:2017-06-01 11:06:52

标签: swift

我在使用小数位的现有应用中有自定义键盘。我正在尝试实施帝国测量。

所以我从键盘上删除了小数点并添加了一个选择器视图,我希望在1/32之间有所有可能的分数 - > 31/32。 这就是它现在的样子。除了选择器(在" Done"按钮上方)之外,所有功能都可以按预期使用UITextfield: Custom Keyboard

我无法将[Strings]加载到选择器中。以下是我对键盘的协议和自定义类。

protocol imperialDelegate: class {
    func iKeyWasTapped(_ character: String)
    func iKeyDone()
    func iBackspace()
    func iKeyCalc()
    func iFraction()
}

class imperial: UIView, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var picker: UIPickerView!

    var pickerData: [String] = [String]()

    let fractions = ["1/32","1/16","3/32","1/8","5/32","3/16","7/32","1/4","9/32",
                     "5/16","11/32","3/8","13/32","7/16", "15/32","1/2","17/32",
                     "9/16", "19/32","5/8","21/32","11/16", "23/32","6/8","25/32",
                     "13/16","27/32", "7/8","29/32","15/16","31/32"]

    // MARK:- keyboard initialization

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.picker.delegate = self
        self.picker.dataSource = self
        initializeSubviews()
        picker.reloadAllComponents()
        picker.reloadInputViews()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeSubviews()
    }

    func initializeSubviews() {
        let xibFileName = "imperialKeyboard"
        let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)?[0] as! UIView
        self.addSubview(view)
        view.frame = self.bounds
    }

    // MARK:- Button actions from .xib file

    @IBAction func keyTapped(_ sender: UIButton) {
        self.delegate?.iKeyWasTapped(sender.titleLabel!.text!)
    }

    @IBAction func backspace(_ sender: UIButton) {
        self.delegate?.iBackspace()
    }

    @IBAction func Done(_ sender: UIButton) {
        self.delegate?.iKeyDone()
    }

    @IBAction func keyCalc(_ sender: UIButton) {
        self.delegate?.iKeyCalc()
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return fractions.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return fractions[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.delegate?.iFraction()
    } 
}

我从另一个在UITextFields中使用此键盘的视图中调用这些协议函数。

  1. 我希望选择器填充[Fractions]
  2. 我想了解如何将选择器显示的字符串值存储到我调用它的viewcontroller中的变量中。
  3. 我对初始化没有很好的理解,这是我想象的问题所在。对不起代码转储。

1 个答案:

答案 0 :(得分:0)

所以我制定了显示器。这只是重新安排代码的问题:

required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initializeSubviews()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeSubviews()

        self.picker.delegate = self
        self.picker.dataSource = self
        picker.reloadAllComponents()
        picker.reloadInputViews()        
    }

我想问题是我试图在它存在之前填充它。

这是我从其他视图控制器获取值的方法。首先,我将协议编辑为这样,因此需要输入字符串。

protocol imperialDelegate: class {
    func iKeyWasTapped(_ character: String)
    func iKeyDone()
    func iBackspace()
    func iKeyCalc()
    func iFraction(_ character: String)
}

然后pickerview didSelectRow函数如下所示:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    self.delegate?.iFraction(fractions[row])
}

然后剩下的就是调用viewController中的函数,以便自定义键盘上的值可以像这样发送文本字段:

func iFraction(_ character: String) {
    for tf in textFields {
        tf.insertText(character)
    }
}

因此,当您登陆选择器中的值时,didSelectRow函数会将该字符串发送到协议,然后将其发送到ViewController,并在其中将值显示在文本字段中。

这就是它的样子。

enter image description here