如何将文本附加到活动的UITextField-Swift

时间:2018-11-26 21:59:51

标签: swift

我正在尝试制作自定义键盘。但是我无法在活动textField上输入信息。我不确定自己在做什么错。

PS:键盘位于另一个ViewController中,并且顺利传递了sender.tag。

这是我的代码:

import UIKit

class HomeVC: UIViewController, ButtonTapDelegate, UITextFieldDelegate {

@IBOutlet var textField1: UITextField!
@IBOutlet var textField2: UITextField!
@IBOutlet var keyboardView: UIView!

var activeField: UITextField?
var delegate: ButtonTapDelegate!

override func viewDidLoad() {
    addKeyboard(view: keyboardView)
    textField1.inputView = UIView()
    textField2.inputView = UIView()
    textField1.becomeFirstResponder()
    activeField?.delegate = self
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    activeField = textField
}
func addKeyboard(view: UIView) {
    let keyboard = KeyboardVC(nibName: "KeyboardVC", bundle: nil)
    keyboard.delegate = self
    view.addSubview(keyboard.view)
    addChild(keyboard)
}

func didTapButton(sender: UIButton) {
    if sender.tag == 8 {
        activeField?.text?.append(contentsOf: " ")
    } else if sender.tag == 9 {
        activeField?.text?.removeAll()
    } else {
        let val = sender.titleLabel?.text
        activeField?.text?.append(contentsOf: val!)
    }
}
}

2 个答案:

答案 0 :(得分:0)

您的代码中有一个小问题导致了您的问题。在评论中,您提到didTapButton()被称为之前 textFieldDidBeginEditing。这意味着actionField未分配值,因此为nil。您在didTapButton()中的代码可以安全地取消包装可选值,从而不会产生任何错误,但是,当然,您不能将文本追加到不存在的UITextField上。

答案 1 :(得分:0)

经过研究后,我能够通过更改 textFieldDidBeginEditing didTapButton 函数来修复此问题:如果有人想一次选择一个textField,这是完整的代码使用自定义键盘:


import UIKit

class HomeVC: UIViewController, ButtonTapDelegate, UITextFieldDelegate {

@IBOutlet var textField1: UITextField!
@IBOutlet var textField2: UITextField!
@IBOutlet var keyboardView: UIView!

var activeField: UITextField?
var delegate: ButtonTapDelegate!

override func viewDidLoad() {
    addKeyboard(view: keyboardView)
    textField1.inputView = UIView()
    textField2.inputView = UIView()
    textField1.becomeFirstResponder()
    activeField?.delegate = self
}

func textFieldDidBeginEditing(_ textField: UITextField) {
   self.activeField = textField
}

func addKeyboard(view: UIView) {
    let keyboard = KeyboardVC(nibName: "KeyboardVC", bundle: nil)
    keyboard.delegate = self
    view.addSubview(keyboard.view)
    addChild(keyboard)
}

func didTapButton(sender: UIButton) {
        if textField1 == self.activeField {
            if sender.tag == 8 {
                textField1.text?.append(contentsOf: " ")
            } else if sender.tag == 9 {
                textField1.text?.removeAll()
            } else {
                let val = sender.titleLabel?.text?
                textField1.text?.append(contentsOf: val!)
            }
            return;
        }

    if textField2 == self.activeField {
        if sender.tag == 8 {
            textField2.text?.append(contentsOf: " ")
        } else if sender.tag == 9 {
            textField2.text?.removeAll()
        } else {
            let val = sender.titleLabel?.text?
            textField2.text?.append(contentsOf: val!)
        }
        return;
    }
}
}